I have created a test WCF service WcfSampleLib with contract as IWcfSampleLib and ny service class is clsWcfSampleLib.
namespace WcfSampleLib
{
public class clsWcfSampleLib:IWcfSampleLib
{
public string getMsg(string name)
{
return " HI " + name;
}
}
[ServiceContract]
public interface IWcfSampleLib
{
[OperationContract]
string getMsg(string name);
}
}
Now I have added a window form application to host my WCF. I have created a App.config as
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Beh">
<serviceMetadata httpGetEnabled="true" httpGetUrl="http://{server}:9097"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name ="WcfSampleLib.clsWcfSampleLib" behaviorConfiguration="Beh">
<endpoint address="http://{server}:9096/SomeName" binding="basicHttpBinding" contract ="WcfSampleLib.IWcfSampleLib"/>
<endpoint address="net.tcp://{server}:9095/SomeName1" binding="netTcpBinding" contract ="WcfSampleLib.IWcfSampleLib"/>
</service>
</services>
</system.serviceModel>
</configuration>
I have two endpoints to use WCF from two different clients with different end point.
I have added following lines of code in Form1_Load event of Host Application
host = new ServiceHost(typeof(WcfSampleLib.clsWcfSampleLib));
host.Open();
MessageBox.Show("started");
Now I can add reference of service with http://{server}:9097 only. Is there any way so that I can use both endpoints with different URL means net.tcp://{server}:9095/SomeName1 and http://{server}:9096/SomeName
Related
My App.config file is
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="WcfJsonRestService.Service1">
<endpoint address="http://localhost:8733/service1"
binding="webHttpBinding"
contract="WcfJsonRestService.IService1"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
My service1.cs code is as below
using System;
using System.ServiceModel.Web;
namespace WcfJsonRestService
{
public class Service1 : IService1
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "data/{id}")]
public Person GetData(string id)
{
// lookup person with the requested id
return new Person()
{
Id = Convert.ToInt32(id),
Name = "Leo Messi"
};
}
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
}
Initially this was giving issue as
WCF Service Host Configuration - Please try changing the HTTP port to 8733
So I had followed Executing the following code in CMD
netsh http add urlacl url=http://+:8733/ user=WORK\Clara
After executing this code I am facing new error as below
How can I resolve this issue?
I have also tried updating the App.Config as said over below link but then after I was getting some another error
WCF service host cannot find any service metadata
You are missing service metadata behavior configuration. Please add below configuration:
<configuration>
<system.serviceModel>
<services>
<service name="WcfJsonRestService.Service1">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733"/>
</baseAddresses>
</host>
<endpoint address="service1"
binding="webHttpBinding"
contract="WcfJsonRestService.IService1"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled ="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
I'm not fluent in WCF Services so I'm struggling to return XmlElement as the return type.
I'm getting the message from the WCF Test Client (running in debug mode):
The operation is not supported from the wcf test client because it uses type XmlElement.
[ServiceContract]
public interface IClientService
{
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Xml)]
XmlElement GetClientXml(int value);
}
namespace testWCF
{
public class testSvc: IClientService
{
public XmlElement GetClientXml(int value)
{
string appDir = AppContext.BaseDirectory;
XmlDocument xDoc = new XmlDocument();
xDoc.Load(appDir + #"Xml\ResponseTempl.xml");
return xDoc.DocumentElement;
}
}
}
I've referred to this as well but it might be too old, as I'm using 4.6.1 framework: Returning XML From a WCF Service
my Web.Debug.config file :
<?xml version="1.0" encoding="utf-8"?>
<!-- For more information on using web.config transformation visit https://go.microsoft.com/fwlink/?LinkId=125889 -->
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
</system.web>
<system.serviceModel>
<services>
<service name="AucklandRegionalPatientWCF.PatientDemographicService" >
<!-- these endpoint are necessary to return SOAP service -->
<endpoint address=""
binding="basicHttpBinding"
contract="AucklandRegionalPatientWCF.IPatientDemographicService" />
<endpoint address="mex"
contract="IMetadataExchange" binding="mexHttpBinding"/>
<!-- REST service return xml -->
<!--To call this endpoint use: [service].svc/xml/[method_Name]-->
<endpoint address="xml"
binding="webHttpBinding" behaviorConfiguration="xmlBehavior"
contract="AucklandRegionalPatientWCF.IPatientDemographicService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<!-- use XML serialization -->
<behavior name="xmlBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
Best way to get XML element from WCf as response use XML formatted string.. Best at performance as well as light weight.....
Create response class and use XML serialization.... It will be helpful
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 WCF rest webservice. It is working fine. I am wanting to understand the different configuration values available within the endpoint element.
In particular, I'm trying to understand the purpose of the address element. Changing the value doesn't seem to change how I can address the service. For this, I'm running the service from visual studio 2010 and cassini. the port number is set to 888.
with address set to an empty string i get...
http://localhost:888/restDataService.svc/hello will return "hello world".
with address set to "localhost" i get...
http://localhost:888/restDataService.svc/hello will return "hello world".
with address set to "pox" i get...
http://localhost:888/restDataService.svc/hello will return "hello world".
It doesn't matter what value I set into the address field. It doesn't impact the url. My only explanation that I have is that the value is more for non-REST services.
<system.serviceModel>
<services>
<service behaviorConfiguration="MobileService2.DataServiceBehaviour" name="MobileService2.DataService">
<endpoint address="pox" binding="webHttpBinding" contract="MobileService2.IRestDataService" behaviorConfiguration="webHttp">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="MobileService2.DataServiceBehaviour" >
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
I also have the following service contract
[ServiceContract]
public interface IRestDataService
{
[OperationContract]
[WebGet(UriTemplate = "hello")]
string Hello();
}
And in the .svc
<%# ServiceHost Language="C#" Debug="true"
Service="MobileService2.RestDataService"
Factory="System.ServiceModel.Activation.WebServiceHostFactory"
CodeBehind="RestDataService.svc.cs" %>
And the 'code-behind'
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RestDataService : IRestDataService
{
public string Hello()
{
return "hello";
}
}
Can you also show service element of your configuration? I think that your configuration is not used or you are accessing other instance of the application (did you configure Cassini to use port 80?) because your second and third test should return HTTP 404 Resource not found.
Correct addresses for your tests are:
http://localhost/restDataService.svc/hello
http://localhost/restDataService.svc/localhost/hello
http://localhost/restDataService.svc/pox/hello
Check that your name in service element is exactly the same as name of service type (including namespaces) as used in ServiceHost directive in .svc markup.
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.