开发者

POST Method in wcf Rest Service [duplicate]

This question already has an answer here: Closed 10 years ago.

Possible Duplicate:

POST Method in wcf Rest Service

I am not able to use POST Method in wcf Rest service.Please help to the following code

Interface Implementation

[OperationContract]
[WebInvoke( UriTemplate = "/SendMail",Method ="POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
EmailDetails SendMail(EmailDetails rData); 

protected void Button1开发者_JAVA技巧_Click(object sender, EventArgs e)
        {
            BasicHttpBinding binding = new BasicHttpBinding();
            binding.ReaderQuotas.MaxStringContentLength = 2000000;
            binding.ReaderQuotas.MaxNameTableCharCount = 2147483647;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.CloseTimeout = new TimeSpan(4, 0, 0);
            binding.OpenTimeout=new TimeSpan(4, 0, 0);
            binding.ReceiveTimeout=new TimeSpan(2, 0, 0);
            binding.SendTimeout = new TimeSpan(5, 0, 0);
            EndpointAddress endpoint = new EndpointAddress(new Uri("http://localhost:35798/RestServiceImpl.svc"));
            RestPostService.RestServiceImplClient obj = new RestPostService.RestServiceImplClient(binding, endpoint);
            RestPostService.EmailDetails obj1 = new RestPostService.EmailDetails();
            obj.ClientCredentials.Windows.ClientCredential = System.Net.CredentialCache.DefaultNetworkCredentials;
            RestPostService.EmailDetails obj2=obj.SendMail(obj1);  
        } 

It is returning a error:remote server not found and sometimes The server did not provide a meaningful reply; this might be caused by a contract mismatch, a premature session shutdown or an internal server error.


If you are the owner and consumer of the webservice, try removing the WebInvokeAttribute. Or at least removing the UriTemplate. Looking solely at the provided code in the button click, it's not needed and possibly causing your problem.


Put this in your web.config file, It will help you figure out why your web service is not working:

<system.diagnostics>     
    <sources> 
      <source name="System.ServiceModel" 
              switchValue="Information, ActivityTracing" 
              propagateActivity="true"> 
        <listeners> 
          <add name="traceListener" 
              type="System.Diagnostics.XmlWriterTraceListener" 
              initializeData= "c:\temp\WEBTraces.log" /> 
        </listeners> 
      </source> 
    </sources> 
  </system.diagnostics> 

Read more of this here: http://msdn.microsoft.com/en-us/library/ms733025.aspx


REST works on simple Http protocol with Get/Post/Put/Delete verbs.

BasicHttpBinding is for SOAP based web services.

In order to make a request to a WCF service using the POST verb of Http protocol you need to use either WebRequest class or some 3rd party libraries like RestSharp.

When using REST style the client and server do not have any means of proxy between them to perform a request as it is in a SOAP based web service.

Have a look at the below link to build some REST WCF services and how to use them

  1. Building WCF Rest Service

  2. Invoke the WCF Rest Service using .NET Client

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜