0

I have web service that I can consume successfully, but I am sharing my webservice with someone else who wants to input the parameters via the URL eg: //localhost:12345/Lead.asmx?op=SendFiles&Id=1234678&Name=Joe&Surname=Kevin

I added :

<webServices>
      <protocols>
        <add name="HttpGet"/>
      </protocols>
    </webServices>

to my Web.Config file and my SendFile.asmx.cs code looks like this:

    namespace SendFiles
   {
       /// <summary>
       /// Summary description for Service1
       /// </summary>
    [WebService(Namespace = "http://testco.co.za/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class SendFile : System.Web.Services.WebService
    {

        [WebMethod]
        public bool PostToDB(LoadEntity _lead)
        {

            ConnectToSQLDB(ConfigurationManager.AppSettings["Server"],   ConfigurationManager.AppSettings["DB"],
                                ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["Password"], ref connectionRef);

            if (LI.ImportFiles(_lead, ref (error)) == true)
            {
                return true;
            }
            else
                return false;
        }

I tried adding :

 [OperationContract]
    [WebGet]
    bool PostToDB(string IDNo, string FName, string SName);

But I get an error that I must declare a body because it is not marked abstract, extern or partial. Can anyone help?

user1668123
  • 105
  • 1
  • 1
  • 10

2 Answers2

1

In response to your request on how to create a WCF Rest Service...

In your service contract:

[ServiceContract]
public interface ITestService
{
    [WebGet(UriTemplate = "Tester")]
    [OperationContract]
    Stream Tester();
}

On your implementation

public class TestService : ITestService
{
    public Stream Tester()
    {
        NameValueCollection queryStringCol = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters;

        if (queryStringCol != null && queryStringCol.Count > 0)
        {
            string parameters = string.Empty;
            for (int i = 0; i < queryStringCol.Count; i++)
            {
                parameters += queryStringCol[i] + "\n";
            }

            return new MemoryStream(Encoding.UTF8.GetBytes(parameters));
        }
        else
            return new MemoryStream(Encoding.UTF8.GetBytes("Hello Jersey!"));
    }
}

This simply prints out all your query string values. You can do whatever processing you'll need to do depending on what query string parameters you get.

For example if you put in.

http://localhost:6666/TestService/Tester?abc=123&bca=234

Then you'll get

123 234

As your output.

Here's the rest of the code if you still need it. this was built using a console app but it can easily be converted to web. The real import stuff are the one's above.

class Program
{
    static ServiceHost _service = null;

    static void Main(string[] args)
    {
        _service = new ServiceHost(typeof(TestService));
        _service.Open();

        System.Console.WriteLine("TestService Started...");
        System.Console.WriteLine("Press ENTER to close service.");
        System.Console.ReadLine();

        _service.Close();
    }
}

<configuration>
<startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
      <service name="ConsoleApplication1.TestService">
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:6666/TestService"/>
          </baseAddresses>
        </host>
        <endpoint binding="webHttpBinding" contract="ConsoleApplication1.ITestService"
          behaviorConfiguration="webHttp"/>
      </service>      
    </services>
    <bindings>
      <webHttpBinding>
        <binding name="webHttpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647">
          <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior>          
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="webHttp">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>
michaelalm
  • 200
  • 10
0

When you test via the test harness in the .asmx page, what URL is generated? Can you give that to your caller and verify their ability to execute the same url you did?

I would recommend a WCF REST based service if others using your service from non .NET clients is your main use case.

gudatcomputers
  • 2,822
  • 2
  • 20
  • 27
  • I am very new to this. Please can you send me an example on how to use the WCF REST service? – user1668123 Oct 24 '12 at 14:08
  • you can start here http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd assuming you are using Visual Studio 2010. If not, take a look at the WebInvoke attribute http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx . You need to register your service classes in the RouteTable as part of Application_Start in the global.asax.cs – gudatcomputers Oct 24 '12 at 14:33
  • I am using VS2010, and downloaded the Template, but I have no idea how to use it. Is there any documentation anywhere? – user1668123 Oct 25 '12 at 05:39