I'm trying to build my first WCF service. I've got the following behavior now.
When I run my WCF service, I can send in input and get the right results in Testing Client.
When I type http://localhost:12345/Service1.svc into Chrome, I get a page.
Clicking on svcutil.exe http://localhost:12345/Service1.svc?wsdl gives me an XML.
However, when I type http://localhost:12345/Service1.svc/test/13, I only get an empty response. There's nothing in there but <body> with a <pre>. What can I be doing wrong and how do i resolve it? (Keep in mind that I'm a rookie at this.) Once I'll get the behavior working the way I want (so I can see the right result in the browser) I'll be producing either REST or JSON data in XML format (if that's of any importance).
From this discussion I got this.
namespace WcfService1
{
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "/test/{indata}",
ResponseFormat = WebMessageFormat.Xml)]
String Ping(String indata);
}
}
As can be seen in this question my implementation is as follows.
namespace WcfService1
{
public class Service1 : IService1
{
public string Ping(String indata)
{
return "Pong " + indata;
}
}
}
The suggested web.config didn't work so I've tried to publish metadata (whatever that is) using the pointers in this article in combination with this discussion. My configuration file look pretty much as the one in the latter link (except that I've removed the diagnostic part).
I believe that the WCF testing client operates on SOAP. It tests more the fact that you're serving something, than that you're serving what you'd like to get.
The empty body you're getting is, according to my experience, nothing but an error message. However, under some circumstances, such as cross domain calls (not sure if it's the correct name nor if it's the full list of possible issues), when you work with e.g. XDomainRequest object in JavaScript (as opposed to the usual XmlHttpRequest), the response being empty is a result of an error message.
Did you try to check for the status code? Is it 200 OK or something (much) larger?
I believe that you've asked a similar question on Social MSDN and that There was some confusion as how to form the code. Let me recap the highlights below.
Web.config
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
...
</services>
<behaviors>
</behaviors>
</system.serviceModel>
</configuration>
services - contents of the tag describing the service's nature
<service name="DemoRest.RestService"
behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding"
contract="DemoRest.IRestService"
behaviorConfiguration="web"></endpoint>
</service>
behaviors - contents of the tag describing the behavior of the service and the end-point
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
Now the response can be retrieved using the following URL.
localhost/Service1.svc/inputData
You can try following:
Mark service implementation with ServiceBehaviour attribute
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class Service1 : IService1
Inside the web.config add/modify following preserving existing data:
<services>
<service name="WcfService1.Service1">
<endpoint binding="webHttpBinding" contract="WcfService1.IService1" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
...
</behaviors>
These steps makes it working.
To Get REST working using the dot net framework 4 simplified configuration, your web.config needs to contain the following:
<system.serviceModel>
<!-- 1) Specify webHttp as an endpoint behavior -->
<behaviors>
<endpointBehaviors>
<behavior >
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<!-- 2) Setup a protocol mapping to allow the service to be accessed via webHttp-->
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
</system.serviceModel>
To get the output without all the xml in the browser, add this:
<!-- Configure the webHttp standard endpoint -->
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
To allow access to the service metadata (needed to create proxies easily), add this to the behaviours element:
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
Personally I prefer the old style configuration where you explicitly configure the endpoints, which would look like this:
<system.serviceModel>
<services>
<service name="WcfService1.Service1">
<endpoint address="" binding="webHttpBinding" contract="WcfService1.IService1" behaviorConfiguration="webHttpBehavior"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Finally, you can use the WCF Service Configuration Editor to do the configuration using a gui. You can find it on the tools menu of Visual Studio. Once open, open the web.config for your project with it and start editing.
Related
I have added a RESTful WCF service inside a Web application(Righclicked solution and added WCF service) and while running it is exposing the url as svcutil.exe http://localhost:62783/Service1.svc?wsdl but i have tried calling that service UriTemplate from a RESTCLIENT like http://localhost:62783/AuthenticateUser it is throwing an error like
HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
But if i create a seperate RESTful WCF service and calling from a RESTCLIENT is working fine.Here is my code
[OperationContract]
string AuthenticateUser1();
and
[WebInvoke(UriTemplate = "/AuthenticateUser", Method = "GET", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
public string AuthenticateUser1()
{
return string.Format("Token {0}", new Guid().ToString());
}
and config
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Any suggestion??
Based on your posted config, you have a default endpoint for SOAP of basicHttpBinding, which is (by default) mapped to the http scheme. I've done very little with REST, but I believe you will need to add an endpoint using webHttpBinding to do REST, and most likely the URL will need to be http://localhost:62783/Service1.svc/AuthenticateUser (note the inclusion of the service file), though I'm not 100% sure on that one.
To add a REST endpoint, do something like this in your service's config file:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<!-- Added for REST -->
<endpointBehaviors>
<behavior name="REST">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<!-- REST endpoint -->
<endpoint address="" binding="webHttpBinding"
contract="<contract name with namespace>"
behaviorConfiguration="REST">
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
Again, REST is not my strong point, but this should hopefully get you pointed in the right direction at least.
Im trying to host to different service implementations of the same contract:
The reason is that need a dummy implementation for out-of-the-house testing.
Im trying to host both in the same WindowsService:
private ServiceHost _host;
private ServiceHost _dummy;
protected override void OnStart(string[] args)
{
_host = new ServiceHost(typeof(Service));
_host.Open();
//trying to avoid the app.config beeing used - because its already been hoste by _host
_dummy = new ServiceHost(typeof(TestDummyService));
_dummy.Description.Endpoints.Clear();
_dummy.AddServiceEndpoint(typeof(IService),
new WebHttpBinding(),
#"<link>/Dummy.svc/");
_dummy.ChannelDispatchers.Clear();
_dummy.Open();
}
This is the config file:
<system.serviceModel>
<services>
<service name="namespace.Service">
<host>
<baseAddresses>
<add baseAddress="<link>/Service.svc"/>
</baseAddresses>
</host>
<endpoint address=""
binding="webHttpBinding"
contract="namespace.IService"
behaviorConfiguration="web" />
<endpoint address="/mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors >
<behavior>
<serviceMetadata httpGetEnabled="true"
httpGetUrl="<link>/Service.svc/About" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name ="web">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
The ChannelDispatcher at /Service.svc/About with Contracts ‘IHttpGetHelpPageAndMetadataContract’ is unable to open.
any help is appreciated.
Update 1
My goal is to have 2 different implementations of the same contract (IService) hosted in one WindowsService.
I would also like to configure both of them in the config file.
Well i would like to know what's the business scenario. All I guess is, the client should not know the implementation, its just the URL of the service would indicate (or route) to the implementation.
Kindly clarify.
Refer to this existing post and let
me know if it makes sense.
The above post is hinting the implementation, refer to this post for deployment details.
so i found out, that even thow the testdummy service was added programatic, it still got the service metadatabehavior.
My solution was to not make the dehavior default - given it at name:
app.config:
<service name="namespace.Service" behaviorConfiguration="someName">
//.. later:
<behavior name="someName">
<serviceMetadata httpGetEnabled="true"
httpGetUrl="<link>/Service.svc/About" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
The rest of the code, statyed the same
Can't you add another endpoint and fill in the adress with a distinct name:
<endpoint address="/SecondService"
binding="webHttpBinding2"
contract="namespace.IService"
/>
Url becomes /Service.svc/SecondService
I am writing a WCF service, (json REST) and I have it working fine when using the wcftestclient.exe
When I run that test tool it triggers my break points while debugging and everything works as expected.
but, when using a browser to navigate to the the service and method, no break point is triggered. it seems as though the request isnt even getting to the code.
I receieve no errors on when navigating with web browser to the service, it just doesn't get any data, or trigger the break points.
Apologies if this is a duplicate, I have read and tried many many different configurations found in answers to similar questions, but nothing seems to work.
Many thanks for any help, I've posted my code below.
Martyn
I have setup:
ServiceContract
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
List<Country> GetAllCountries();
The Service CLass:
public List<Country> GetAllCountries()
{
ControlServiceRepository rep = new ControlServiceRepository();
return rep.GetAllCountries().ToList() ;
}
and my web config
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="OmniData" behaviorConfiguration="ServiceConfig">
<!-- Service Endpoints -->
<host>
<baseAddresses>
<add baseAddress="http://localhost:55641/"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="ControlService.IOmniData" behaviorConfiguration="rest" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceConfig">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false" defaultOutgoingResponseFormat="Json"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
I think there are some things missing in your contract
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetAllCountries", RequestFormat = WebMessageFormat.Json,ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
List<Country> GetAllCountries();
Try this.Let me know if it helps.
I got this working in the end by deleting all the end points in the config and using
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(OmniData)));
if anyone else has issues, this is even easier than setting up end points because you can just specify the type of responses and end points within the classes themselves.
so:
Add a global.asax if one does exist and include this:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("", new WebServiceHostFactory(), typeof(OmniData)));
}
decorate your Service class with
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
here is mine:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class OmniData : IOmniData
{
public Country[] GetAllCountries()
{
ControlServiceRepository rep = new ControlServiceRepository();
return rep.GetAllCountries().ToArray() ;
}
}
then the interface you setup your endpoing and types using WebGet or WebInvoke
public interface IOmniData
{
[OperationContract]
[WebGet(UriTemplate = "OmniData/GetAllCountries", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
Country[] GetAllCountries();
}
the UriTemplate is the end point, so to access the method you would use: http://MyService.com/OmniData/GetAllCountries
and finally, web config
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="false"/>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="OmniData">
<!-- Service Endpoints -->
<host>
<baseAddresses>
<add baseAddress="http://localhost:55641"/>
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" contract="ControlService.IOmniData" behaviorConfiguration="rest" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="rest">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Alot of help from here
but, importantly for what I wanted, json results, you need to make sure:
automaticFormatSelectionEnabled="false" is in there so it will use the response format specified in the interface. Otherwise you end up with XML instead.
hopefully this helps someone else
And thanks again for fiddler!
Martyn
Hello and thank you for reading.
I'm trying to get a service hosted in IIS 7.5, that has multiple endpoints exposed.
I have a feeling the problem lies within my web.config, but I'll post my service code in here. There's no interface file, as I'm using the newer features of WCF 4, there's also no .svc file.
All the routing, from my understanding is handled in Global.asax.cs using the RouteTable feature.
Regardless, onto the code / config -
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
// NOTE: If the service is renamed, remember to update the global.asax.cs file
public class Service1
{
// TODO: Implement the collection resource that will contain the SampleItem instances
[WebGet(UriTemplate = "HelloWorld")]
public string HelloWorld()
{
// TODO: Replace the current implementation to return a collection of SampleItem instances
return "Hello World!";
}
}
And now, the config with the changes I thought would need to be made (I'm not sure if I needed to keep the standardEndpoints block, but with or without it I'm still getting error messages. -
<services>
<service name="AiSynthDocSvc.Service1" behaviorConfiguration="HttpGetMetadata">
<endpoint name="rest"
address=""
binding="webHttpBinding"
contract="AiSynthDocSvc.Service1"
behaviorConfiguration="REST" />
<endpoint name="soap"
address="soap"
binding="basicHttpBinding"
contract="AiSynthDocSvc.Service1" />
<endpoint name="mex"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="HttpGetMetadata">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
The Global.asax.cs file was left alone.
Again I'm pretty sure it has something to do with my config. The error I'm getting when I try to access any of the endpoints defined is -
The endpoint at '' does not have a Binding with the None MessageVersion. 'System.ServiceModel.Description.WebHttpBehavior' is only intended for use with WebHttpBinding or similar bindings.
Anyone have any ideas on this one?
Thanks,
Zachary Carter
OK, I tried to replicate your stuff - works like a charm for me :-)
I used your service class - no changes
I used your RegisterRoutes call in global.asax.cs
When I launch the web app from within Visual Studio, I get Cassini (the built-in web server) come up on http://localhost:3131/ - this might wary in your case.
Now, I can easily navigate there with a second browser window, and I do get a simple response on this URL:
http://localhost:3131/Service1/HelloWorld
+--------------------+
from Cassini
+--------+
name (first param) in ServiceRoute registration
+-----------+
from your URI template on the WebGet attribute
Does the same URL work for you??
Update: here's my config - I can connect to http://localhost:3131/Service1/HelloWorld in the browser using REST, and I can connect to http://localhost:3131/Service1/soap with the WCF Test Client to make a SOAP call (my Service1 lives in the RestWebApp namespace - thus my service and contract names are a tad different than yours - but other than that, I believe it's identical to your own config):
<system.serviceModel>
<serviceHostingEnvironment
aspNetCompatibilityEnabled="true" />
<services>
<service name="RestWebApp.Service1" behaviorConfiguration="Meta">
<endpoint name="rest"
address=""
binding="webHttpBinding"
contract="RestWebApp.Service1"
behaviorConfiguration="REST" />
<endpoint name="SOAP"
address="soap"
binding="basicHttpBinding"
contract="RestWebApp.Service1" />
<endpoint name="mex"
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Meta">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<standardEndpoints>
<webHttpEndpoint>
<!--
Configure the WCF REST service base address via the global.asax.cs file and the default endpoint
via the attributes on the <standardEndpoint> element below
-->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
</system.serviceModel>
Thanks for this it helped me a lot.
The issue in my case was that I had a default behaviour configured that contains webHttp. After giving it the name="REST" and setting my webHttpBinding endpoint behaviourConfiguration ="REST" I had no further errors.
<system.serviceModel>
<bindings>
<customBinding>
<binding name="CustomBinding_IMobileService">
<binaryMessageEncoding />
<httpTransport />
</binding>
</customBinding>
</bindings>
<client>
<endpoint address="http://localhost:6862/silverlight/services/MobileService.svc"
binding="customBinding" bindingConfiguration="CustomBinding_IMobileService"
contract="AlchemyMobileService.IMobileService" name="CustomBinding_IMobileService" />
</client>
<services>
<service name="MobileService.Alchemy">
<endpoint address="http://localhost:8732/mobileservice" binding="webHttpBinding" contract="MobileService.IAlchemy" behaviorConfiguration="REST">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
I don't know much about WCF. But I have a very basic service that I'm trying to execute. My service code looks like this:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
[ServiceBehavior(IncludeExceptionDetailInFaults = false)]
[ServiceContract]
public class MyService
{
[OperationContract]
[WebGet(UriTemplate = "/IsValidRequest")]
public bool IsValidRequest()
{
return true;
}
}
Like I said, a very basic service. When I enter "http://localhost:[port]/MyService.svc" into my browser, I see the service description page. However, "IsValidRequest" is not listed like I thought it would be (maybe this only happens with .asmx's). Either way, when I enter "http://localhost:[port]/MyService.svc/IsValidRequest" into my browser, nothing is returned. In Fiddler, I see that I get an HTTP 400 error. However, there is nothing that gives me any inkling as to what the problem could be.
Can someone help me out and point me in the right direction? Thanks!
Use the following config (change the namespaces to match your code)
<services>
<service name="WcfService1.MyService" behaviorConfiguration="GetBehavior">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="WcfService1.MyService">
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="GetBehavior">
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="false" aspNetCompatibilityEnabled="true"/>
Your .svc file should look like:
<%# ServiceHost Language="C#" Debug="true" Service="WcfService1.MyService" CodeBehind="MyService.svc.cs" %>
Looking at the WSDL for the message is irrelevant because as you have said in a response to a comment you want this to be a REST based service and WSDL is a SOAP construct. On that basis you should remove a <serviceMetadata> behavior if you have one as this is about SOAP metadata.
To diagnose problems like this you should turn on tracing in WCF (I have a short screencast here that shows you how to do it). This should highlight what the problem is in processing the message
To wire up the REST plumbing without adding a section in the config for your service add the following to your config file under the system.serviceModel section
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>