Strange error with WCF REST Services

2019-08-04 04:17发布

I have suddenly started facing the below issue. My REST Service no more works. It was working sometime ago and I didn't make any changes to config. As of sudden, it is behaving weird. I used WCF Test client to check the issue and strangely, the service method gets called 6 times and finally throws the below error.

Error: Cannot obtain Metadata from http://localhost:49796/Test.svc/GetInformation?memberId=1 If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost:49796/Test.svc/GetInformation?memberId=1 Metadata contains a reference that cannot be resolved: 'http://localhost:49796/Test.svc/GetInformation?memberId=1'. The remote server returned an unexpected response: (405) Method Not Allowed. The remote server returned an error: (405) Method Not Allowed.HTTP GET Error URI:

http://localhost:49796/Test.svc/GetInformation?memberId=1

There was an error downloading 'http://localhost:49796/Test.svc/GetInformation?memberId=1'.    The underlying connection was closed: An unexpected error occurred on a receive.    Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.    An existing connection was forcibly closed by the remote host

The most surprising part is that it is same even if I create a new service or test an old service. Did something went wrong in my pc globally? It appears to be some file corruption or change in config globally somewhere as all of WCF REST Services in my pc behaves the same. I googled but could not find any proper answer.

I tried removing end point and tested again using Test Client. As test client adds basichttpbinding automatically, it is working fine using test client.

Please advise on the fix, if anyone is aware.

my config looks like below:

<system.serviceModel>
<services>
  <service name="TestService.ServiceLayer.Services.TestService" behaviorConfiguration="TestBehavior">
    <endpoint address="" binding="webHttpBinding" 
              contract="TestService.ServiceLayer.Services.ITestService" behaviorConfiguration="web" />
   </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="web">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="TestBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
    <behavior name="">
      <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>
  </serviceBehaviors>
</behaviors>

My Service contract is as below

 [ServiceContract]
public interface ITestService
{
    [OperationContract]
    [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, UriTemplate = "GetInformation?memberId={memberId}")]
    List<MyTestClass> GetInformation(string memberId);
}

I have analyzed further and found that if I change List to string it works fine. I added KnownType as well on top of service contract but it doesn't work. Please advise on this line.

标签: .net wcf rest
3条回答
迷人小祖宗
2楼-- · 2019-08-04 04:49

In your configuration, I don't see the Mex binding. Without that, I don't think the Metadata can be obtained from the service. Mex refers to mexHttpBinding

Roughly speaking, it looks something like this:

<services>
   <service name="MyService" behaviorConfiguration="MEX">
    <endpoint
       address="http://localhost:8000/MEX"
       binding="mexHttpBinding"
       contract="IMetadataExchange"        />
    </service>
</services>

<behaviors>
   <serviceBehaviors>
      <behavior name="MEX">
        <serviceMetadata/>
      </behavior>
    </serviceBehaviors>
</behaviors>
查看更多
在下西门庆
3楼-- · 2019-08-04 04:56

The resolution is mind-blowing. I see one of the data contract fields is NULL in the DB but it is not Nullable type in C#. It is resulting in the strange behavior my REST Service.

查看更多
男人必须洒脱
4楼-- · 2019-08-04 05:01

Enable wcf tracing and see the trace log in SvcTraceViewer.exe.Check out following link for how to enable trace in wcf.

4 simple steps to enable tracing in WCF

How to use SvcTraceViewer

Using Service Trace Viewer for Viewing Correlated Traces and Troubleshooting

查看更多
登录 后发表回答