Converting WCF SOAP Service Library to REST Service library for MVC - wcf

Problem Statement:
We have built MVC 4 application using WCF Service library (not WCF Service Application with svc file). All of the MVC CRUD operations we are doing through WCF SOAP Service library.Everything is working fine.Now suddenly client has changed requirement and asking us to convert WCF SOAP to REST.
I went through many URL's for converting WCF SOAP to REST.In all the URL's they have mentioned converting WCF SOAP Service Application to WCF REST not the WCF SOAP Service library to WCF REST.
My question is can we convert WCF Service library in SOAP to REST.??Is Conversion is same for both WCF Service library and WCF Service Application.??If possible does it impact too much,in changing the existing code??
It would be great if anyone can suggest the better way of converting WCF SOAP to REST in MVC with some URL's and explanation.

Not sure if it helps, but i have done WCF library which is exposed to Soap and REST at the same time. REST exposes XML/JSON and return type depends on input request.
It can be configured via config file, mine looked like this:
<behaviors>
<serviceBehaviors>
<behavior name="BehaviourService">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="BehaviourEndPoint"></behavior>
<behavior name="BehaviourWebHttp">
<webHttp defaultBodyStyle="Wrapped" faultExceptionEnabled="True" automaticFormatSelectionEnabled="True"/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="NameSpace.MyClass" behaviorConfiguration="BehaviourService" >
<!--SoapUI-->
<endpoint name="Name" address ="soap" binding="basicHttpBinding" contract="NameSpace.IMyClass"></endpoint>
<!--Rest-->
<endpoint name="Name" address="" binding="webHttpBinding" behaviorConfiguration="BehaviourWebHttp" contract="NameSpace.IMyClass" ></endpoint>
<!--MEX-->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:89/MyAddress"/>
</baseAddresses>
</host>
</service>
</services>
And service like:
[ServiceContract(Namespace = "http://www.mycompany.com", Name = "MyName")]
[DataContractFormat]
public interface IMyClass
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "MyMethod", Method = "POST")]
bool MyMethod();
}

Related

Is there way to convert just some service methods in a WCF to webMethods? Or add webmethods to an existing WCF? [duplicate]

Background
I have created ASMX web services in the past and have been able to access the service from the web browser and Ajax GET requests using the address convention: MyService.asmx/MyMethod?Param=xxx
I just got started using WCF and created a new web service in my ASP.NET project. It creates a file with the .svc extension such as MyService.svc.
Current Situation
I am able to consume the service using the WcfTestClient that comes with VS2008. I am also able to create my own WCF Client by either adding a service reference in another project or using the svcutil.exe commandline to generate the proxy and config file.
The Problem
When I try to use the service from a browser using MyService.svc/MyMethod?MyParam=xxx, I get a blank page without any errors.
What I have tried
I have already added a basicHttpBinding to the web.config and made it HttpGetEnabled in the behavior configuration. I also added the [WebGet(UriTemplate = "MyMethod?MyParam={MyParam}")] attribute to my operation contract.
I have already followed the information in this other stack overflow question:
REST / SOAP EndPoints for a WCF Service
However, I either get a blank page or an HTTP 404 Error after following those steps. There's nothing special about the code. I am just taking in a string as a parameter and returning "Hello xxx". This is a basic "Hello WCF World" proof-of-concept type thing.
UPDATE - Here's the relevant code
[ServiceContract]
public interface IMyService
{
[WebGet(UriTemplate = "MyMethod/MyParam={MyParam}")]
[OperationContract]
string MyMethod(string MyParam);
}
Web.Config - system.serviceModel Section
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="MyService">
<endpoint address=""
binding="wsHttpBinding" contract="IMyService" />
<endpoint address="MyService.svc"
binding="basicHttpBinding" contract="IMyService" />
<endpoint address="mex"
binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
Looking at your web.config serviceModel section, I can see that you need to add a webHttpBinding and associate an endPointBehavior that includes webHttpGet.
Your operation contract is correct. Here's how your system.serviceModel config section should look in order for you to be able to consume the service from a GET HTTP request.
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MyServiceBehavior" name="MyService">
<endpoint address="ws" binding="wsHttpBinding" contract="IMyService"/>
<endpoint address="" behaviorConfiguration="WebBehavior"
binding="webHttpBinding"
contract="IMyService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
Be sure to assign a different address to your wsHttpBinding endpoint, otherwise you will get an error saying that you have two endpoints listening on the same URI.
Another option is to leave the address blank in the wsHttpBinding, but assign a different address to the webHttpBinding service. However, that will change your GET address as well.
For example, if you assign the address as "asmx", you would call your service with the address "MyService.svc/asmx/MyMethod?MyParam=xxxx".
The normal WCF requests are always SOAP requests - you won't be able to get this going with just your browser, you'll need the WCF Testclient for that.
There is an add-on for WCF called the WCF REST Starter Kit (which will also be included in WCF 4.0 with .NET 4.0), which allows you to use GET/POST/PUT/DELETE HTTP commands to query WCF services and such. You need to write your services specifically for REST, though - you can't have SOAP and REST on the same service call.
Marc
As marc_s says, the REST Starter Kit can help, but you should also be aware that .NET 3.5 has support for REST services directly in it. It's not quite as complete as what you can do with the starter kit, but it is useful.
The way it works is that you put a [WebGet] attribute on your operations to indicate where in the URL the various parameters should come from:
[WebGet(UriTemplate = "helloworld/{name}")]
string Helloworld(string name);
See this portal for tons of information.
Note, you can have the same service exposed as both SOAP and REST if you specify multiple endpoints/bindings in the configuration.

How to publish WSDL for WCF 4.0 service with REST/SOAP endpoints

I'm creating a WCF4 service with REST and SOAP endpoints to be hosted in IIS 7.5.
I have used the WCF4 REST template as an example.
However I have a few questions regarding my setup so far.
Here's my webconfig
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MEXGET">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="MEXGET" name="Project.WebService.VehicleService">
<endpoint
address=""
behaviorConfiguration="REST"
binding="webHttpBinding"
contract="Project.WebService.IVehicleService" />
<endpoint
address="soap"
binding="basicHttpBinding"
contract="Project.WebService.IVehicleService" />
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
I have removed the standardEndpoints section and added endpoints of my own.
There are no .svc files as I've set the routes in the global.asax as shown below
private void RegisterRoutes()
{
RouteTable.Routes.Add(new ServiceRoute("VehicleService", new WebServiceHostFactory(), typeof(VehicleService)));
}
The help pages can be accessed via http://localhost:1313/ServiceTest/VehicleService/help
I've also used the WCF Test Client to access http://localhost:1313/ServiceTest/VehicleService/mex
which shows the metadata for the SOAP endpoint
But how do I retrieve the WSDL of the service?
With an svc file I can find the wsdl at http://localhost:1313/ServiceTest/VehicleService.svc?wsdl However I do not have a .svc file.
And neither can I find the WSDL at http://localhost:1313/ServiceTest/VehicleService?wsdl or http://localhost:1313/ServiceTest/VehicleService/soap?wsdl
Do I need to add a .svc file if I want to publish WSDL for the SOAP endpoint?
I have managed to get the WSDL working at http://localhost:1313/ServiceTest/VehicleService?wsdl.
The solution was using new ServiceHostFactory instead of new WebServiceHostFactory in the global.asax.
With WebServiceHostFactory you lose the WSDL functionality.
A similar question can be found here:
ServiceRoute + WebServiceHostFactory kills WSDL generation? How to create extensionless WCF service with ?wsdl
You should be able to get the WSDL by using the mex URL directly from the browser:
http://localhost:1313/ServiceTest/VehicleService/mex
The WcfTestClient is almost certainly doing that to retrieve the WSDL so it can generate the proxy to the service.

WCF Server - asynchronous method

Im new in WCF.
I have WCF service with method
public string DoSomething(int i);
I call this method from one client and next client can`t get results from this method until the first client finishes method.
How to make this calls asynchronous?
My WCF Service:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession, ConcurrencyMode = ConcurrencyMode.Multiple)]
public class MyService : IMyService
My config:
<system.serviceModel>
<services>
<service name="MyService" behaviorConfiguration="ServiceBehavior">
<endpoint contract="IMyService" binding="basicHttpBinding"/>
<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<!--true-->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
If you want to make asynchronous calls to your service:
In Visual Studio Solution Explorer navigate to your Service Reference in the the project that contains your client application. Right click your Service reference and choose "Configure Service Reference", then check the box "Generate Asynchronous Operations", then click OK.
It shouldn't be necessary though because you can configure your service to handle multiple requests. You may want to look at how you have configured InstanceContextMode and ConcurrencyMode. The basicHttpBinding doesn't support sessions so your service is probably defaulting to InstanceContextMode.PerCall. For more details see Sessions, Instancing, and Concurrency
Maybe you have something limiting the number of calls. For more details see ServiceThrottlingBehavior

Two endpoints in WCF 3.5 for SOAP and JSON

I dont know what I am doing wrong. I have a WCF (.NET 3.5) service (JsonSoap.svc) that has two endpoints for soap and json content type. Both the endpoints refer to the same service. I am using only one Json endpoint in the client. My aim is to have the service method GetPerson() to return Json or soap depending on the endpoints used to connect to the service (hopefully this is possible in WCF?). I can see wsdl and was able to successfully ad the service reference in to the client side.
I get the following error after I make a call to the GetPerson() -
"An error occurred while receiving the
HTTP response to
http://localhost:80/JsonSoap/json/GetPerson.
This could be due to the service
endpoint binding not using the HTTP
protocol. This could also be due to
an HTTP request context being aborted
by the server (possibly due to the
service shutting down). See server
logs for more details."
WCF service config
<!-- bindings -->
<bindings>
<basicHttpBinding>
<binding name ="soapBinding">
<security mode="None">
</security>
</binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding">
</binding>
</webHttpBinding>
</bindings>
<!-- JSON behaviors -->
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="defaultBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TestService.IJsonSoap" behaviorConfiguration="defaultBehavior">
<host>
<baseAddresses>
<!-- note, choose an available port-->
<add baseAddress="http://localhost:80/JsonSoap" />
</baseAddresses>
</host>
<endpoint address="soap" binding="basicHttpBinding"
bindingConfiguration="soapBinding"
contract="TestService.IJsonSoap" />
<endpoint address="json" binding="webHttpBinding"
bindingConfiguration="webBinding"
behaviorConfiguration="jsonBehavior"
contract="TestService.IJsonSoap" />
</service>
WCF code:
[OperationContract]
[WebGet]
Person GetPerson(int ID);
WCF Client config:
<endpoint address="http://localhost:80/JsonSoap/json" binding="webHttpBinding"
bindingConfiguration="webBinding" behaviorConfiguration="jsonBehavior"
contract="MyService.IJsonSoap" />
Client code:
MyService.JsonSoapClient service = new JsonSoapClient();
MyService.Person person = service.GetPerson(10);
This will not work. WSDL servers only for SOAP services and it is the source for Add Service Reference in Visual Studio. You are using client code generated by Visual Studio but you are using it with Json endpoint which doesn't work.
Json endpoint represents REST service. To call WCF REST service in .NET you must either:
Build manully HTTP Request
Share service contract with a client and use ChannelFactory or WebChannelFactory to build a proxy
Use REST Starter KIT CTP2 and its HttpClient class (not recommended because development of REST Starter KIT ended).

What is the importance of IMetadataExchange in WCF?

What is the use and importance of IMetadataExchange in WCF?
I have the following app.config file in which I don't use IMetadataExchange endpoint, but I am still able to create my proxy client. I have read that if I don't use IMetadataExchange endpoint, AddServiceReference will not work because my service does not expose the metadata. How is it working without exposing IMetadataExchange endpoint?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="metaDataBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name ="WCFService.Services" behaviorConfiguration="metaDataBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8090/Services/"/>
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="WCFService.IMathOperations"/>
</service>
</services>
</system.serviceModel>
</configuration>
ArsenMkrt has the formal answer. Put more simply:
If you don't have it, adding a service reference will not work
You should delete it from production servers, so that a hacker cannot add a service reference
To answer your question more specifically, you have this line on your service:
<service name ="WCFService.Services" behaviorConfiguration="metaDataBehavior">
Which points to this configuration
<behavior name="metaDataBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
This may be why it still works, although I thought that you needed to specify the MEX endpoint.
IMetadataExchange Interface Exposes methods used to return metadata about a service.
When programming Windows Communication Foundation (WCF) services, it is useful to publish metadata about the service. For example, metadata can be a Web Services Description Language (WSDL) document that describes all of the methods and data types employed by a service. Returning metadata about an WCF service allows consumers of a service to easily create clients for the service.
The difference is:
<serviceMetadata httpGetEnabled="true"/>
allows you to retrieve metadata using the HTTP protocol.
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
allows you to retrieve metadata using the ws-metadata protocol.
Just <serviceMetadata httpGetEnabled="true"/> works, but not all clients can call you (because they can't retrieve metadata to create a proxy).
The standard is to publish both.
See also ServiceMetadataBehavior Class (MSDN).
Without IMetadataExchange, a WCF service exposes the metadata information to the client, but WCF does not guarantee to expose the metadata because WCF default features to exposing the metadata to the client.
Exposing the metadata is done in a well-standardized way through IMetadataExchange. The IMetadataExchange interface follows the industry standard.