I have a simple web service I have created. I'm using Visual Studio 2012 .NET 4.5.
Here is the service contract:
using System.Runtime.Serialization;
using System.ServiceModel;
namespace GEMS.Core.WCFService
{
[ServiceContract]
public interface IMenuService
{
[OperationContract]
void AddMenuItem(string menuId, string parentId, string title, string description, string url);
[OperationContract]
void UpdateMenuItem(string menuId, string parentId, string title, string description, string url);
[OperationContract]
void DeleteMenuItem(string menuId);
[OperationContract]
MenuEntry[] GetAllMenuItems();
}
[DataContract]
public class MenuEntry
{
[DataMember]
public string MenuId { get; internal set; }
[DataMember]
public string ParentId { get; internal set; }
[DataMember]
public string Title { get; internal set; }
[DataMember]
public string Description { get; internal set; }
[DataMember]
public string Url { get; internal set; }
}
}
The relevant portion of the app.config is:
<system.serviceModel>
<services>
<service name="GEMS.Core.WCFService.MenuService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8020/GEMS.Core.WCFService/MenuService/" />
</baseAddresses>
</host>
<endpoint address="" binding="basicHttpBinding" contract="GEMS.Core.WCFService.IMenuService" >
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
I publish it to my IIS server (local to my box).
In the client app, I create a service reference. When I click Discover, it finds:
http://localhost:8020/GEMS.Core.WCFService/MenuService/mex
When I run my client, however, I get the following message:
There was no endpoint listening at:
http://localhost:8020/GEMS.Core.WCFService/MenuService/ that could
accept the message. This is often caused by an incorrect address or
SOAP action. See InnerException, if present, for more details.
The inner exception merely says that it got a 404 error from the web server.
The client's .config file has the following auto-generated xml:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMenuService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8020/GEMS.Core.WCFService/MenuService/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMenuService"
contract="WCF_MenuService.IMenuService" name="BasicHttpBinding_IMenuService" />
</client>
</system.serviceModel>
I've been going around in circles for hours and can't figure out what I've got glitched up, but surely I've done somethign wrong.
Not sure that it matters, but the client is an ASP.NET MVC app.
As Pete explained Newtonsoft.json.dll causes client to not generate the service code. You can either delete the dll and then add the service reference again.
Or you can do try following solution that worked for me. When you add a service reference,
Click on the 'Advanced...' button in the 'Add service reference' - window
Disable 'Reuse types in referenced assemblies'.
Then your classes should be generated.
Related
I'm new to WCF and I've been hitting my head for the past week trying to get everything to work. When browsing the service.svc file I receive the message about the metadata not being enabled. There's hundreds of posts on this but I must be missing something. I think I followed the instructions correctly but I still can't find my error. Where am I going wrong? Any help is appreciated.
service.svc
<%# ServiceHost Service="BiteSizeLearningWS.TranscriptService" Debug="true" %>
web.config
<services>
<service name="BiteSizeLearningWS.iServiceInterface" behaviorConfiguration="TranscriptServiceBehavior">
<endpoint address="" binding="basicHttpBinding" contract="BiteSizeLearningWS.TranscriptService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="TranscriptServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
ServiceContract
namespace BiteSizeLearningWS
{
[ServiceContract (Name="TranscriptService")]
public interface iServiceInterface{...
Implementation
public class TranscriptService : iServiceInterface
Global.asax
namespace BiteSizeLearningWS
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("TranscriptService", new WebServiceHostFactory(), typeof(TranscriptService)));
}
I think you have your:
<service name="BiteSizeLearningWS.iServiceInterface"...
name attribute value and the
<endpoint address="" ... contract="BiteSizeLearningWS.TranscriptService" />
contract attribute value mixed up. Try this:
<service name="BiteSizeLearningWS.TranscriptService"...
and
<endpoint address="" ... contract="BiteSizeLearningWS.iServiceInterface" />
If that works, then what happened is that WCF used the automatic default configuration values for the service instead of the invalid configuration shown in the question. The metadata endpoint is not enabled by default which would be why you're seeing the "disabled" message.
trying to build a RestFull service with wcf running in the WcfTestClient.exe. The problem is that I get an error:
Failed to add a service. Service metadata may not be accessible.
I added a mex endpoint in the config file but does not solve it:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MyRest.Service" behaviorConfiguration="ServBehave">
<!--Endpoint for REST-->
<endpoint
address="XMLService"
binding="webHttpBinding"
behaviorConfiguration="restPoxBehavior"
contract="MyRest.IService"/>
<endpoint
address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServBehave" >
<!-- 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 for the REST endpoint for Help enability-->
<behavior name="restPoxBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
IService1.cs
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "/Employees", ResponseFormat = WebMessageFormat.Xml)]
Employee[] GetEmployees();
}
[DataContract]
public class Employee
{
[DataMember]
public int EmpNo { get; set; }
[DataMember]
public string EmpName { get; set; }
[DataMember]
public string DeptName { get; set; }
}
Service1.cs
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class Service1 : IService1
{
public Employee[] GetEmployees()
{
return new Employee[]
{
new Employee() {EmpNo=101,EmpName="Mahesh",DeptName="CTD"},
new Employee() {EmpNo=102,EmpName="Akash",DeptName="HRD"}
};
}
}
With WCF Restful service, do you actually need meta-data to expose service or to work on it? The answer is "NO". It's against the principles of Rest. Meta-data represents the interface(the operations), and for REST interface is fixed(http methods). WcfTestClient is for testing SOAP based Service(as they have to expose their interface through mex bindings).
Testing a RESTFUL service for http get could be vary easy. you just have to invoke it from your browser, using the URL. To test other http methods, you have to build your custom client.
If this seems a big task, then you could also use tools like Fiddler to build request data. An example could be seen here
I have a simple WCF service defined by the following ServiceContract:
[ServiceContract]
public interface IInventoryService
{
[OperationContract]
Item GetItemFromBarcode(string barcode);
[OperationContract]
string Test(string testString);
}
With Item defined like this:
[DataContract]
public class Item
{
[DataMember]
public virtual int Id { get; set; }
<Snip>
}
And the actual service implemented thusly:
public class InventoryService : IInventoryService
{
[WebGet(UriTemplate = "/Barcode/{barcode}/Item", ResponseFormat = WebMessageFormat.Json)]
public Item GetItemFromBarcode(string barcode)
{
var item = (from b in repository.Query<ItemBarcode>()
where b.BarcodeData == barcode
select b.Item).FirstOrDefault();
return item;
}
[WebGet(UriTemplate = "/Test/{testString}",ResponseFormat=WebMessageFormat.Xml)]
public string Test(string testString)
{
return testString;
}
}
With the following in the app.config for the program hosting the service:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RESTFriendly">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="InventoryService">
<endpoint address="/Inventory" behaviorConfiguration="RESTFriendly" binding="webHttpBinding" contract="IInventoryService"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
Now that the code dump is out of the way, the issue: I can invoke the Test method just fine with curl or Fiddler, it returns a serialized string. However, invoking the method that returns an object returns nothing. Curl spits back curl: (56) Failure when receiving data from the peer and Fiddler responds ReadResponse() failed: The server did not return a response for this request.
From what I read, this should Just Work (tm). Is there something obvious I'm missing?
So, it seems you can't have DataMember of an interface type, like the IList<ItemBarcode>. There go my hopes of using my NHibernate model objects as DTOs.
I have created WCF service but while running the service I am getting below error:
Error:
Failed to add a service. Service
metadata may not be accessible. Make
sure your service is running and
exposing metadata.
Error Details:
Warning: No code was generated.If you
were trying to generate a client, this
could be because the metadata
documents did not contain any valid
contracts or servicesor because all
contracts/services were discovered to
exist in /reference assemblies. Verify
that you passed all the metadata
documents to the tool.Warning: If you
would like to generate data contracts
from schemas make sure to use the
/dataContractOnly option.
Code:
namespace WCFTest
{
[ServiceContract]
public class EmployeeDetails
{
[OperationContract]
public List<Employee> GetDetails()
{
List<Employee> emp = new List<Employee>()
{
new Employee(){Fname="AA",Lname="BB",EmpId=1,Desg="A"},
new Employee(){Fname="CC",Lname="DD",EmpId=1,Desg="B"},
new Employee(){Fname="EE",Lname="FF",EmpId=1,Desg="C"},
new Employee(){Fname="GG",Lname="HH",EmpId=1,Desg="D"},
new Employee(){Fname="II",Lname="JJ",EmpId=1,Desg="A"},
new Employee(){Fname="KK",Lname="LL",EmpId=1,Desg="B"}
};
return emp;
}
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
[KnownType(typeof(Employee))]
public class Person
{
[DataMember]
public string Fname { get; set; }
[DataMember]
public string Lname { get; set; }
}
[DataContract]
public class Employee : Person
{
[DataMember]
public int EmpId { get; set; }
[DataMember]
public string Desg { get; set; }
}
}
namespace WCFTest
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
public class Service1
{
public List<Employee> GetData(int value)
{
EmployeeDetails ed = new EmployeeDetails();
return ed.GetDetails();
}
}
}
However I could see metadata is exposed in web.config.
Web.config:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</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="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Any clue where I am going wrong?
EDIT: I think the reason behind the error is I am using a class as a service contract, now when I change it to an Interface, things work as expected, not sure why I am getting the error if I am specifying the class as service contract.
I don't see any <services> node in your config - you're not configuring a service at all - so there's nothing there to connect to.
You need to extend your config to include something like this:
<configuration>
<system.serviceModel>
<services>
<service name="WCFTest.EmployeeDetails">
<endpoint name="Default"
address="/default"
binding="basicHttpBinding" bindingConfiguration=""
contract="WCFTest.EmployeeDetails" />
<endpoint kind="mexEndpoint" address="/mex" />
</service>
</services>
</system.serviceModel>
</configuration>
Now, you have a service with a service and a metadata endpoint, and now your WCF Test Client should be able to find something to connect to....
We've tried to use a very very simple WCF service with a HTTp Get and we can't get it work.
We've followed those "guide" but it doesn't work
http://msdn.microsoft.com/en-us/library/bb412178.aspx
http://www.dotnetfunda.com/articles/article779-simple-5-steps-to-expose-wcf-services-using-rest-style-.aspx
When we call our service with the following url, we get a page not found error:
http://localhost:9999/Service1.svc/GetData/ABC
The base url (http://localhost:9999/Service1.svc) works fine and returns the wcf service information page correctly.
Those are the steps and code to reproduce our example.
In Visual Studio 2010, create a new "WCF Service Application" Project
Replace the IService interface with this code
[ServiceContract()]
public interface IService1
{
[OperationContract()]
[WebInvoke(Method = "GET",
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "GetData/{value}")]
string GetData(string value);
}
Replace the Service class with this code
public class Service1 : IService1
{
public string GetData(string value)
{
return string.Format("You entered: {0}", value);
}
}
The web.config look like this
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Service1">
<endpoint address="" binding="webHttpBinding" contract="IService1" behaviorConfiguration="WebBehavior1">
</endpoint>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="WebBehavior1">
<webHttp helpEnabled="True"/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
Press Run and try to call the Get method
If someone get this or something similar working, it would be very kind if you could reply information about the working example.
Thank you very much
I recreated your sample - works like a charm.
One point: do your service contract (public interface IService1) and service implementation (public class Service1 : IService1) exist inside a .NET namespace??
If so, you need to change your *.svc and your web.config to include:
<services>
<service name="Namespace.Service1">
<endpoint address="" binding="webHttpBinding"
contract="Namespace.IService1"
behaviorConfiguration="WebBehavior1">
</endpoint>
</service>
</services>
The <service name="..."> attribute and the <endpoint contract="..."> must include the .NET namespace for this to work.