I am trying to create a WCF
service at runtime. My service interface is:
[ServiceContract]
public interface IInformationService : IService
{
[OperationContract]
[WebInvoke(Method = "Get", ResponseFormat = WebMessageFormat.Json,
UriTemplate = "Test", RequestFormat = WebMessageFormat.Json)]
string Test();
}
I am serving my service as follows:
var httpEnumerator = ImplementedContracts.Values.GetEnumerator();
httpEnumerator.MoveNext();
var httpContractType = httpEnumerator.Current.ContractType;
var webBinding = new WebHttpBinding()
{
Security =
{
Mode = WebHttpSecurityMode.None
}
};
var httpEndpoint = AddServiceEndpoint(
httpContractType,
webBinding, baseAddress+/Get"
);
httpEndpoint.Behaviors.Add(new CustomEndpointBehavior());
The ServiceHost is created by this method:
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
var host = new WcfServiceHost(serviceType, baseAddresses);
if (host.Description.Behaviors.Contains(typeof(ServiceDebugBehavior)))
{
(host.Description.Behaviors[typeof(ServiceDebugBehavior)] as
ServiceDebugBehavior).IncludeExceptionDetailInFaults = true;
}
else
{
var debug = new ServiceDebugBehavior
{
IncludeExceptionDetailInFaults = true
};
host.Description.Behaviors.Add(debug);
}
if (host.Description.Behaviors.Contains(typeof(ServiceMetadataBehavior)))
{
(host.Description.Behaviors[typeof(ServiceMetadataBehavior)] as ServiceMetadataBehavior).HttpGetEnabled = true;
(host.Description.Behaviors[typeof(ServiceMetadataBehavior)] as ServiceMetadataBehavior).HttpsGetEnabled = true;
}
else
{
var smb = new ServiceMetadataBehavior
{
HttpGetEnabled = true,
HttpsGetEnabled = true
};
host.Description.Behaviors.Add(smb);
}
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpsBinding(),
"mex"
);
host.AddServiceEndpoint(
ServiceMetadataBehavior.MexContractName,
MetadataExchangeBindings.CreateMexHttpBinding(),
"mex"
);
return host;
}
Service route creation:
var serviceRoute = new ServiceRoute(
"wcf.service/" + service.Value.Name,
new WcfServiceHostFactory(),
service.Value
);
if (!RouteTable.Routes.Contains(serviceRoute))
{
RouteTable.Routes.Add(serviceRoute);
}
When I try to access my service from a web browser using the address
http://localhost/Werp.View/wcf.service/InformationService/Get/Test
I obtain the following error:
<Fault xmlns="http://schemas.microsoft.com/ws/2005/05/envelope/none">
<Code>
<Value>Sender</Value>
<Subcode>
<Value xmlns:a="http://schemas.microsoft.com/ws/2005/05/addressing/none">
a:ActionNotSupported
</Value>
</Subcode>
</Code>
<Reason>
<Text xml:lang="en-US">
The message with Action '' cannot be processed at the receiver, due to a
ContractFilter mismatch at the EndpointDispatcher. This may be because of
either a contract mismatch (mismatched Actions between sender and receiver)
or a binding/security mismatch between the sender and the receiver. Check
that sender and receiver have the same contract and the same binding
(including security requirements, e.g. Message, Transport, None).
</Text>
</Reason>
Can anyone help me?
If you don't need specific WCF features or you have mandate to use WCF you should consider using different stack for REST based services. For example ASP.NET web API or ServiceStack. It looks like a lot of work to do a simple REST call.
If you turn on service diagnostics this might help diagnosing the problem. You can see this SO for detailed instructions
You can also refer to this SO: WCF - ContractFilter mismatch at the EndpointDispatcher exception for some ideas.
My problem has been solved when I added WebHttpBehavior to endpoint
httpEndpoint.Behaviors.Add(new WebHttpBehavior());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With