WCF REST and SOAP Service without WebServiceHostFactory

Despite reading a few posts, for example ( This seems popular ). I cannot show my service as multiple endpoints that are compatible with both SOAP and REST protocol - my problem seems to be related

Factory="System.ServiceModel.Activation.WebServiceHostFactory" 

in the service code behind the page.

If I leave this, my SOAP endpoint works great, but my JSON endpoint is not found. If I put the line, my REST endpoint sings like a bird, and the SOAP endpoint causes the "Endpoint not found" on the Service.svc page.

My operations seem to be configured in a standard way, for example:

  [OperationContract] [WebGet(UriTemplate = "/GetData", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)] string GetData(); 

And the configuration file

  <endpoint address="rest" binding="webHttpBinding" contract=".IMeterService" behaviorConfiguration="REST" /> <endpoint address="soap" binding="wsHttpBinding" contract="IMeterService" bindingConfiguration="secureBasic" /> <behavior name="REST"> <webHttp /> </behavior> 

How can i achieve this? Is there a way to configure a REST endpoint without the System.ServiceModel.Activation.WebServiceHostFactory attribute?

Thanks in advance.

+6
source share
1 answer

If you do not specify any factory in the .svc file, all endpoints will be obtained from the web.config file - WCF will try to find the <system.serviceModel / service> element, the name attribute matches the value > fully qualified class of service. If he does not find it, he will add a default endpoint (using basicHttpBinding if you have not changed the default mapping). This seems to be what you came across. Confirm that the "name" attribute of the <service> element matches the value of the Service attribute in the .svc file, and you must clearly define the two endpoints.

Another thing you can try to do is enable tracing in the service (level = Information) to see which endpoints were actually open in the service. Image below:

SvcTraceViewer image

The server for this example has nothing to do:

 namespace MyNamespace { [ServiceContract] public interface ITest { [OperationContract] string Echo(string text); } public class Service : ITest { public string Echo(string text) { return text; } } } 

Service.svc does not specify factory:

 <% @ServiceHost Service="MyNamespace.Service" Language="C#" debug="true" %> 

And web.config defines two endpoints that are shown on the track:

 <configuration> <system.diagnostics> <sources> <source name="System.ServiceModel" switchValue="Information, ActivityTracing" propagateActivity="true"> <listeners> <add type="System.Diagnostics.DefaultTraceListener" name="Default"> <filter type="" /> </add> <add name="ServiceModelTraceListener"> <filter type="" /> </add> </listeners> </source> </sources> <sharedListeners> <add initializeData="C:\temp\web_tracelog.svclog" type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" name="ServiceModelTraceListener" traceOutputOptions="Timestamp"> <filter type="" /> </add> </sharedListeners> <trace autoflush="true"/> </system.diagnostics> <system.serviceModel> <behaviors> <endpointBehaviors> <behavior name="Web"> <webHttp /> </behavior> </endpointBehaviors> </behaviors> <services> <service name="MyNamespace.Service"> <endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="" name="basic" contract="MyNamespace.ITest" /> <endpoint address="web" behaviorConfiguration="Web" binding="webHttpBinding" bindingConfiguration="" name="web" contract="MyNamespace.ITest" /> </service> </services> </system.serviceModel> </configuration> 

Note that there is an additional listener in the listener, this is the "help page" from the WCF (the one that reports when viewing on it that the service does not have metadata).

You can either try to compare this setting with yours, or start with this simple one, and then start adding the component from yours until you encounter a problem. This will help isolate the problem.

Good luck

+5
source

Source: https://habr.com/ru/post/897911/


All Articles