Configure WCF WebSite to support RESTful access from a browser - c#

I have used VS2012 to create a new WCF website (Add->New Web Site->WCF Service).
'Out of the box' this gives me the following web.config file:
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
It also gives me an initial Service class as follows:
public class Service : IService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
I then F5 to start the project and it fires up a browser window listing the dir contents, including Service.svc.
I want to type in a URL to call the GetData method. What do I type into the browser address bar, and how to I configure and/or decorate the service so I can type a URL into the browser and see a JSON format string being returned?

Mark the Operation in your contract (IService interface) with following attribute - [WebInvoke(Method = "GET", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate="GetData/{id}")]
And then try invoking your service from browser by adding GetData/[any number]

Related

How to make configuration from an xml file for WCF service

I am new here. I want create a WCF WebService using Visual Studio and use it in Xamarin Forms application. This is my ultimate goal. Now I created WCF Service project and starts IIS service on my Mac machine(Windows 8 OS). But When I run the code already given in project to check whether things are alright or not, I am getting following error :
Failed to add a service. Service metadata may not be accessible. Make
sure your service is running and exposing metadata.
My Web.config file is as below :
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
Service.svc file :
<%# ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
Service.cs file :
public class Service : IService
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}

WCF Webservice - Failed to invoke the service. The service is offline or inaccessible

We have developed the wcf service,when we are calling web service from WCF Test Client to fetch the data, In this case we are getting some exception while calling the WCF web service. I want to log error into html file.
My Code:-
try
{
int i = 1;
int j = 0;
int k = i / j;
}
catch (Exception ex)
{
ApplicationError.LogErrors(ex);
}
Web Config :-
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<handlers>
<remove name="ISAPI-dll"/>
<add name="ISAPI-dll" path="*.dll" verb="*" modules="IsapiModule" scriptProcessor="C:\WINDOWS\System32\inetsrv\asp.dll" resourceType="File" requireAccess="None" allowPathInfo="true" preCondition="bitness64"/>
</handlers>
<directoryBrowse enabled="false"/>
</system.webServer>
Error :-
You can't log directly from WCF into HTML (or rather, you could but shouldn't).
What you could rather do is throw a FaultException with an appropriate message and have your ASP.NET page handle the Exception and render an HTML page with the appropriate Exception.Message (I prefer Exception.ToString() ) in it.
For more information have a look at this link: Fault Handling in WCF Services

i am running wcf service not working on iis

I am working on wcf first time so created just sinple wcf ,problem is when i run service from visual studio its working fine but when i deploy it on iis it giving HTTTP 500 Error
my web config file is as below
<?xml version="1.0"?>
<configuration>
<appSettings/>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
<httpRuntime/>
<pages controlRenderingCompatibilityVersion="4.0"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfService2.Service1" behaviorConfiguration="servicebehavior">
<endpoint address="" binding="webHttpBinding" contract="WcfService2.IService1" behaviorConfiguration="web"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="servicebehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
and svc file is as below
namespace WcfService2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
public string JSONData(string id)
{
return "you requested prduct" + id ;
}
public void DoWork()
{
}
}
}
and interface is as below
namespace WcfService2
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "/json/{id}")]
string JSONData(string id);
void DoWork();
}
}
https://stackoverflow.com/a/11460220/2211593
my iis server was not having feature of HTTP activation enabled so after lot of googling i found this solution. very useful for me i spent almost half of day to find the solution of this small issue

WCF recieves a complex object as null

After getting input in another thread here on the forum [StackOverFlow][1]
[1]: Getting a nullreference when passing a complex object from KSOAP2 to WCF have i reached conclusion that my problem is with the WCF service itself. It seems that the service is not able to understand the object that it's receiving.
No matter what I do the object passed is always null.
I have even tried declaring the datamember explicit even though it should not be necessary in the current version of WCF.
Here's the class I'm trying to pass through the WCF:
namespace ModelLayer
{
[DataContract]
public class User
{
[DataMember]
public int Id { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string Mac { get; set; }
public User()
{
}
}
}
My service interface:
[ServiceContract (Namespace = "http://nordahl.org/")]
public interface IWcfFoodAndAllergi
{
[OperationContract]
int InsertUser(User _user);
[OperationContract]
User GetUser(string _mac);
[OperationContract]
int InsertRecipe(Recipe _recipe);
[OperationContract]
List<Recipe> GetRecipeByAllergi(List<Allergies> _allergies);
[OperationContract]
List<Recipe> GetRecipeByType(string _type);
[OperationContract]
List<Recipe> GetRecipeByName(string _name);
}
}
And lastly the webconfig file itself:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" maxRequestLength="1048576"/>
</system.web>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
<fileExtensions allowUnlisted="true">
<remove fileExtension=".cs"/>
<add fileExtension=".cs" allowed="true"/>
</fileExtensions>
</requestFiltering>
</security>
</system.webServer>
<system.serviceModel>
<services>
<service name="WcfFoodAndAllergi.WcfFoodAndAllergi">
<!-- Use a bindingNamespace to eliminate tempuri.org -->
<endpoint address=""
binding ="basicHttpBinding"
bindingNamespace="http://nordahl.org/"
contract="WcfFoodAndAllergi.IWcfFoodAndAllergi"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name ="">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
I had a similar problem to yours when a complex object referenced another complex object in a T4 generated class. If you are using an include in an obtain method, or another method relating to Entity or objects linking complex objects to each other; you may get a circular reference error. I had a friend show me how to fix this with a custom class that sets up an attribute:
Complex Object won't return when using 'include' syntax in WCF with Entity Version 6

405 response error from .net WCF Service

I'm trying to write a backend for my app in asp.net but I can't get my WCF service return a POST response after Ajax request from Ext. Instead I'm getting a 405 'Method not allowed' error.
This is my request :
Ext.Ajax.request({
type: 'POST',
url: 'http://localhost:35798/RestServiceImpl.svc/export',
params: {
html : {'array': document.body.innerHTML}
},
success : function(response){
console.log('RESPONSE !', response);
//me.onSuccess(response, callback, errback);
},
failure : function(response){
console.log('RESPONSE FAIL !', response);
}
});
This is my interface :
namespace RestService
{
public class RestServiceImpl : IRestServiceImpl
{
#region IRestServiceImpl Members
public string JSONData()
{
return "Your POST request";
}
#endregion
}
}
and the service code :
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Web.Script.Services;
namespace RestService
{
[ServiceContract]
public interface IRestServiceImpl
{
[OperationContract]
[ScriptMethod]
[WebInvoke(Method = "POST",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "export")]
string JSONData();
}
}
And finally the config :
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="RestService.RestServiceImpl" behaviorConfiguration="ServiceBehaviour">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address ="" binding="webHttpBinding" contract="RestService.IRestServiceImpl" behaviorConfiguration="web">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
This may be a cross-domain issue. E.g if your service is at http://localhost:35798 and the web page making the AJAX request is at http://localhost:80 , it's still considered a different domain by the browser (even though only the port number is different). Some browsers in this case issue a CORS request, and if your service doesn't handle it, will return a 405. Your options then are to either (1)avoid cross-domain requests, (2)implement CORS (will not help browsers that don't support it), or (3) use JSONP.

Categories

Resources