I have a very simple WCF service that is hosted within a windows application. I'm tring to access the service by using GET method but just getting an empty page. I guess there is something wrong with configuration but I could't figure out what. Here is the codes:
WCFService.cs
namespace WCFServiceApp
{
[ServiceContract]
public class WCFService
{
[OperationContract]
[WebGet]
public int GetNumbers()
{
return 12345;
}
}
}
Form1.cs
private static Uri baseAddress = new Uri("http://localhost:8090");
private ServiceHost host = new ServiceHost(typeof(WCFService), baseAddress);
protected override void OnLoad(EventArgs e)
{
ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
host.Description.Behaviors.Add(smb);
host.Open();
base.OnLoad(e);
}
App.config
<?xml version="1.0"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="WebBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WCFService" behaviorConfiguration="WCFServiceBehavior">
<endpoint address="ws" binding="wsHttpBinding" contract="WCFService">
</endpoint>
<endpoint address="" behaviorConfiguration="WebBehavior" binding="webHttpBinding" contract="WCFService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
</configuration>
When I hit localhost:8090 with browser I see WCF welcome page. When I hit localhost:8090/GetNumbers just getting an empty page instead of the numbers.
I have a WCF service with Net.Tcp binding, my server configuration is
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="Service.PlainUserNameValidator, Service" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="Service.TestService">
<endpoint address="" binding="netTcpBinding" contract="Service.ITestService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8732/Service/TestService/" />
<add baseAddress="http://localhost:8001/service/TestServiceMex/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
In the configuration I turned on customUserNamePasswordValidatorType. The code to start the host is
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(TestService)))
{
host.Open();
Console.WriteLine("The service is ready.");
Console.WriteLine(String.Format("Metadata is at {0}?WSDL", host.Description.Endpoints[0].Address));
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
}
}
}
And my custom username validator class registered in config file is
namespace Service
{
using System;
using System.IdentityModel.Selectors;
public class PlainUserNameValidator : UserNamePasswordValidator
{
public override void Validate(string userName, string password)
{
Console.WriteLine("Requesting username {0} and password {1}.", userName, password);
}
}
}
However the validator never seems to fire while the client is calling.
Any special trick I need to notice to enable customUserNamePasswordValidatorType for Net.Tcp binding?
Two points:
You havent included a certificate, you should do so to ensure the integrity of the client's credentials, set up a temporary one if you have to and add this to your service credentials.
<serviceCertificate
findValue="localhost"
x509FindType="FindBySubjectName"
storeLocation="CurrentUser"
storeName="My" />
You also havent specified any security - for this to work, both client and service endpoints need to enable Username and Password authentication mode on their bindings
<netTcpBinding>
<binding name="tcpWithMessageSecurity">
<security mode="Message" >
<message clientCredentialType="UserName"/>
</security>
</binding>
</netTcpBinding>
then
<endpoint address="" binding="tcpWithMessageSecurity" contract="Service.ITestService">
Then, name your behavior and add it in the above line.
Any ideas how I correct this.. calling a service via js
The message with To 'http://MySite.svc/GetStateXML' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree
thanks
The error listed below indicates that the Web Service implements WS-Addressing.
"The message with To '' cannot be processed at the receiver, due to an AddressFilter mismatch at the EndpointDispatcher. Check that the sender and receiver's EndpointAddresses agree"
Include the following in your SOAP Headers to access the Web Service:
<soap:Header xmlns:wsa="http://www.w3.org/2005/08/addressing">
<wsa:To>http://example.com/service</wsa:To>
</soap:Header>
I just ran into this as well while going through an example in the Learning WCF book by Bustamante.
I had used the WCF Config Editor to fill out my config on my host and had put the value in the name attribute for my endpoint rather than the address attribute. Once I fixed it things worked.
I found another post that suggested using:
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
on the implementation class, which worked but wasn't the root cause.
Bottom line appears to be: make sure your client and server configs match.
I got this error while I was using webHttpBinding.
I resolved it by adding
<endpointBehaviors>
<behavior name="EndPointBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
under <behaviors> and setting behaviorConfiguration="EndPointBehavior" in my endpoint with binding="webHttpBinding".
Full config:
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="EndPointBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WcfService1.Service1" behaviorConfiguration="ServiceBehavior">
<endpoint address=""
binding="webHttpBinding"
contract="WcfService1.IService1"
behaviorConfiguration="EndPointBehavior">
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
I know it sounds silly but for anyone else that has this error check your address. We were getting this error because we had a double slash where there should have only been one.
http://localhost//servicename.svc
The above address caused the problem.
http://localhost/servicename.svc
Did not exhibit the problem.
We were dynamically creating the full address from parts of data read in from windows forms and a database. The user was entering /servicename.svc instead of servicename.svc
I had this issue in my development environment for a web service hosted in IIS. Solved it by going to 'IIS Manager' and added a binding to the host name complained about in the error message.
Add webHttp attribute to your config:
endpointBehaviors
behavior name ="yourServiceContract"
webHttp automaticFormatSelectionEnabled ="true "
behavior
I had the same kind of issue, just to be complete :
I used visual studio 2017
I needed to create a wcf service on an old application using .net 3.5
It should be exposed as a soap service and as a "Rest" service
the configuration that I needed to use (for rest part) was :
there is a config for the service behaviour and the endpoint behaviour
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="myServiceBehaviour">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webEndpointBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="My.Service" behaviorConfiguration="myServiceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="My.IService" behaviorConfiguration="webEndpointBehaviour">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>
In my case I have WCF Service library application which is a RESTFul and Windows Service Host. I had problem with Host App.Config. Please see below
WCF Rest Service App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="MyService.DocumentWCFRESTService" behaviorConfiguration="defaultServiceBehavior">
<!-- Service Endpoints -->
<!-- Unless fully qualified, address is relative to base address supplied above -->
<endpoint address="http://localhost:2023/DocumentWCFRESTService"
binding="webHttpBinding" behaviorConfiguration="webBehaviorConfiguration"
contract="MyService.IDocumentWCFRESTService" >
<!--
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.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8733/Design_Time_Addresses/Document.Server.WCFREST.Service/DocumentWCFRESTService/" />
</baseAddresses>
</host>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<bindings> </bindings>
<behaviors>
<serviceBehaviors>
<behavior name="defaultServiceBehavior">
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:2023/DocumentWCFRESTService"/>
<!-- 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="webBehaviorConfiguration">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
Windows Service Host App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingConfiguration" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
maxArrayLength="2147483647" maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="documentWCFRESTServiceBehavior">
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpGetUrl="http://localhost:2023/DocumentWCFRESTService"/>
<!-- 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="webBehaviorConfiguration">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="MyService.DocumentWCFRESTService" behaviorConfiguration="documentWCFRESTServiceBehavior">
<endpoint address="http://localhost:2023/DocumentWCFRESTService" behaviorConfiguration="webBehaviorConfiguration"
binding="webHttpBinding" bindingConfiguration="webHttpBindingConfiguration"
contract="MyService.IDocumentWCFRESTService"></endpoint>
</service>
</services>
</system.serviceModel>
</configuration>
if you have multiple endpoints in your WCFService.config like:
<endpoint address="urn:Service.Test" .../>
<endpoint address="urn:Service.Test2".../>
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:1234/Service/" />
<add baseAddress="http://localhost:1233/Service/" />
<add baseAddress="net.pipe://localhost/Service/" />
</baseAddresses>
</host>
You need to set EndpointAddress like in your config file.
Then you need ClientViaBehavior for the baseAddress.
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
EndpointAddress address = new EndpointAddress("urn:Service.Test");
AndonClient client = new AndonClient(binding, address);
client.ChannelFactory.Endpoint.EndpointBehaviors.Add(new ClientViaBehavior(new Uri("net.tcp://localhost:1234/Service/Test")));
var response = client.GetDataAsync().Result;
For .net core you need to write the Behavior by yourself:
public class ClientViaBehavior : IEndpointBehavior
{
Uri uri;
public ClientViaBehavior(Uri uri)
{
if (uri == null)
throw new ArgumentNullException(nameof(uri));
this.uri = uri;
}
public Uri Uri
{
get { return this.uri; }
set
{
if (value == null)
throw new ArgumentNullException(nameof(value));
this.uri = value;
}
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
if (clientRuntime == null)
{
throw new ArgumentNullException(nameof(clientRuntime));
}
clientRuntime.Via = this.Uri;
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
throw new NotImplementedException();
}
void IEndpointBehavior.Validate(ServiceEndpoint serviceEndpoint)
{
}
}
I had a similar problem.
The following address was provided to the client:
https://test.mycompany.ru/ChannelService/api/connect.svc
But according to the logs, the client sent requests to the following address:
https://test.mycompany.ru/ChannelService/api/connect.svc/SOAP/Adapter
After I added the endpoint address to the configuration file, the service started working:
<services>
<service name="connect">
<endpoint address="/SOAP/Adapter"
binding="basicHttpBinding"
bindingConfiguration="secureHttpBinding"
contract="IContract">
</endpoint>
<endpoint address="mex"
binding="mexHttpsBinding"
contract="IMetadataExchange"/>
</service>
</services>
The key elements are the webHttp and binding="webHttpBinding" for the Json work in the browser test. However SoapUI still failed to return JSon.
Look at <webHttp />
<services>
<service name="SimpleService.SimpleService" behaviorConfiguration="serviceBehaviour">
<endpoint address="" binding="webHttpBinding" contract="SimpleService.ISimpleService" behaviorConfiguration="web">
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange">
</endpoint>
</service>
</services>
....
....
<endpointBehaviors>
<behavior name="web">
<webHttp />
</behavior>
</endpointBehaviors>
i created a WCf service with a method that can receive GET requests using WebGET attribute, i want the same method to receive Soap calls too (that when the programmer does a Service reference to the WCF, he will be able to call the method).
my interface is:
[ServiceContract]
public interface IService1
{
[OperationContract]
[WebGet(UriTemplate = "GetData?value={value}")]
string GetData(int value);
}
My configuration is:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<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 name="WCFTestingGetService.Service1" behaviorConfiguration="MyServiceBehavior" >
<endpoint address="" binding="webHttpBinding" contract="WCFTestingGetService.IService1" behaviorConfiguration="WebBehavior"></endpoint>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Can i make teh web method GetData a HTTP GET and SOAP method ?
what do i need to add to the Config ?
You can use REST and SOAP in the same service but in the case of SOAP the operation will be called with HTTP POST. Your contract is defined correctly. You have to modify your configuration:
<services>
<service name="WCFTestingGetService.Service1" behaviorConfiguration="MyServiceBehavior" >
<endpoint address="" binding="webHttpBinding" contract="WCFTestingGetService.IService1" behaviorConfiguration="WebBehavior"/>
<endpoint address="soap" binding="basicHttpBinding" contract="WCFTestingGetService.IService1"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
I would delete the webget attribute and use basichttpbinding. Then you can access the service with any soap or wcf client.
You can also host an asmx besides the svc, but that doesn't feel quiet right.
Regards,
Michel
I have written a service that I would like expose both via rest and soap. Everything I read about WCF 4.0 says that I just need to expose 2 endpoints with differing behaviors to do this. But I cannot get it to work.
Here is my service contract:
[ServiceContract]
public interface MyService
{
[OperationContract]
[WebGet(UriTemplate="data/{value}")]
string GetData(string value);
}
Here is my web.config:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="MyService">
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>
<endpoint address="rest" behaviorConfiguration="restBehavior" binding="webHttpBinding" contract="MyService" />
<endpoint address="soap" behaviorConfiguration="soapBehavior" binding="basicHttpBinding" contract="MyService" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp automaticFormatSelectionEnabled="true" helpEnabled="true" />
</behavior>
<behavior name="soapBehavior" />
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
I am using routing to define my service url:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("dns", new ServiceHostFactory(), typeof(MyService)));
}
}
Is there something that I am doing wrong here? I could really use some help.
I never found the "right" way to do this in configuration but was able to use the routing engine to accomplish this.
My global asax file now looks like this:
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(new ServiceRoute("my/soap", new ServiceHostFactory(), typeof(MyService)));
RouteTable.Routes.Add(new ServiceRoute("my/rest", new WebServiceHostFactory(), typeof(MyService)));
}
}
and my config like this: (to enable the rest help pages)
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint automaticFormatSelectionEnabled="true" helpEnabled="true"/>
</webHttpEndpoint>
</standardEndpoints>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
I like that this is in line with the asp.net MVC model more and requires little config. Additionally doing it this way allowed me to remove the .svc files from my project entirely which is also a plus IMO.
How are you hosting your WCF service?? In IIS, you need a virtual directory and a MyService.svc file somewhere to enable service activation.
If you remove the ServiceRoute for now (to simplify matters), you should be able to reach your SOAP service endpoint at:
http://YourServer:Port/YourVirtualDirectory/YourService.svc/soap
and your REST service should be at
http://YourServer:Port/YourVirtualDirectory/YourService.svc/rest/data/{value}
(where you supply some arbitrary value for {value}).
What exactly is not working in your case??
You can try and test your SOAP endpoints using the WCF Test Client, while you should be able to hit the REST url in any browser.
This is possible to do in configuration. From msdn forum thread by user Ladislav Mrnka: http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4e95575f-1097-4190-80dd-7a0f96d73f6e
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="iSell.Prospects.ProspectBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="iSell.Prospects.ProspectBehavior" name="iSell.Prospects.ProspectService">
<endpoint address="" behaviorConfiguration="REST" binding="webHttpBinding" contract="iSell.Prospects.ProspectService" />
<endpoint address="soap" binding="basicHttpBinding" contract="iSell.Prospects.ProspectService" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
</system.serviceModel>