I have a wcf (.net 4.5) with one service and multiple interfaces\end points.
This service is declared as follows:
<service name="MyService.Service1">
<endpoint address="Try1" behaviorConfiguration="restfulBehvaiour"
binding="webHttpBinding" contract="MyService.IService1" />
<endpoint address="Try2" behaviorConfiguration="restfulBehvaiour"
binding="webHttpBinding" contract="MyService.ITry" />
</service>
...
<behavior name="restfulBehvaiour">
<webHttp helpEnabled="true" />
</behavior>
I am trying to return any exception as json. I have followd the tutorial on http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/
In short:
1) On the svc file, added this (it implements both interfaces)
<%# ServiceHost Language="C#" Debug="true" Service="MyService.Service1" CodeBehind="Service1.svc.cs" Factory="MyService.CustomWebServiceHostFactory"%>
2) where CustomWebServiceHostFactory is
public class CustomWebServiceHostFactory : System.ServiceModel.Activation.WebServiceHostFactory
{
public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{
var sh = new ServiceHost(typeof(Service1), baseAddresses);
sh.Description.Endpoints[0].Behaviors.Add(new CustomWebHttpBehavior());
return sh;
}
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return base.CreateServiceHost(serviceType, baseAddresses);
}
3) and the custom CustomWebHttpHandler is
protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// clear default error handlers.
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();
// add our own error handler.
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandlerEx());
}
4) and the ErrorHandlerEx is some class that handles the exceptions (returns json object).
this all work great for the first end point (Try1), but the second one (Try2) is being ignored and not going threw the CustomWebServiceHostFactry.
If I switch the order of the endpoints in web.config, the first one always works and the seconds exceptions are being handled by the default wcf handlers.
How can I fix this behaviour, so that every end point will work as the above tutorial suggests?
You only implement the behavior on one endpoint (the first one) in your custom service host.
sh.Description.Endpoints[0].Behaviors.Add(new CustomWebHttpBehavior());
Endpoints[0] is the first endpoint in the collection. You need to add it to both (or all if you have more than 2) endpoints for the service. I recommend a foreach loop:
foreach (ServiceEndpoint endpoint in sh.Description.Endpoints)
{
endpoint.Behaviors.Add(new CustomWebHttpBehavior());
}
This should resolve the issue of the behavior only being applied to the first endpoint.
Related
i am new in wcf. i am facing this error ServiceHost only supports class service types.
here i will say i am doing & running my win service & wcf together.
i added windows service project and also add few reference like System.ServiceModel for wcf in win service project. when i am trying to run wcf service from win service then i am getting error called ServiceHost only supports class service types
i search & got many answer like
ServiceHost host = new ServiceHost(
typeof(subservice.ISubService), new Uri("someuri"));
If this is your usage, change it to use the implemented service class type of ISubService
ServiceHost host = new ServiceHost(
typeof(subservice.SubService), new Uri("someuri"));
If configuring the service in .svc then:
<%#ServiceHost Service="subservice.SubService"%>
Also in you config file, change service name to the service instead of the service contract as:
<services>
<service name="subservice.SubService">
...
other search result also said very similar things to get rid of this problem.
i have no svc file for my wcf service. i have just one file where i have contract and service classes. i also have config file.
here i am giving the brief of my service code
namespace SageDataImportWCF
{
[ServiceContract]
public interface ISagePart
{
[OperationContract]
string SageInsertionProcess(string SQLConnectionString, string CountryCode);
// TODO: Add your service operations here
}
public class SagePartInsertion : ISagePart
{
public string SageInsertionProcess(string SQLConnectionString, string CountryCode)
{
}
}
}
here i am giving the code by which i am trying to run from win service
namespace SageDataImportWCF
{
public partial class SageDateInsertionService : ServiceBase
{
#region Local Variables
ServiceHost serviceHost;
#endregion
#region Constructor
public SageDateInsertionService()
{
InitializeComponent();
serviceHost = null;
ServiceName = "Sage DataInsertion Service";
}
#endregion
protected override void OnStart(string[] args)
{
string strAdrHTTP = "http://192.168.6.2:11000/SagePartInsertion";
if (serviceHost != null)
{
serviceHost.Close();
}
serviceHost = new ServiceHost(typeof(SageDataImportWCF.SagePartInsertion));
serviceHost.AddServiceEndpoint(typeof(SageDataImportWCF.ISagePart), new BasicHttpBinding(), strAdrHTTP);
ServiceMetadataBehavior behaviour = new ServiceMetadataBehavior();
behaviour.HttpGetEnabled = true;
serviceHost.Description.Behaviors.Add(behaviour);
serviceHost.Open();
}
protected override void OnStop()
{
if (serviceHost != null)
{
serviceHost.Close();
serviceHost = null;
}
}
}
}
here is my config entry for wcf service
<configuration>
<system.serviceModel>
<services>
<service name="SageDataImportWCF.SagePartInsertion" behaviorConfiguration="SageBehavior">
<endpoint address="http://localhost:9001/SagePartInsertion" contract="SageDataImportWCF.ISagePart" binding="basicHttpBinding"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="SageBehavior">
<!-- 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="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
here i have pasted all the relevant code and i like to request some one please have a look at my code and tell me why i am getting the error message like ServiceHost only supports class service types when try to run it from windows service. did i miss anything in code ?
should i have a separate project for wcf class library and another separate project for windows service because i have one project there i have files for wcf & windows service both.
so looking for suggestion like what i need to rectify in code as a result win service can start the wcf service. please help.
Check the definition of the service in the Markup:
Right click on the SagePartInsertion.svc file and select "View Markup".
Make sure the service is the implementation of the interface, like this:
<%# ServiceHost Language="C#" Debug="true" Service="SageDataImportWCF.SagePartInsertion" CodeBehind="SagePartInsertion.svc.cs" %>
In the past it failed because I was referencing the interface.
I have some one-time initialization tasks to do on service startup, and in order to do this I have defined a custom ServiceHostFactory and ServiceHost to override InitializeRuntime.
Service1.svc markup:
<%# ServiceHost Language="C#" Debug="true" Factory="service.Our_Service_Host_Factory" Service="service.Service1" CodeBehind="Service1.svc.cs" %>
Service Host definitions:
public class Our_Service_Host_Factory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new Our_Service_Host(serviceType, baseAddresses);
}
}
class Our_Service_Host : ServiceHost
{
public Our_Service_Host(Type serviceType, Uri[] baseAddresses)
: base(serviceType, baseAddresses) { }
protected override void InitializeRuntime()
{
// one time setup code
}
}
Snippet from web.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
</system.serviceModel>
If I remove the Factory attribute on the ServiceHost markup, the WCF Test Client can successfully connect to my service. If I put it back in, it can't find the metadata endpoint with the following error:
Error: Cannot obtain Metadata from http://localhost:50154/Service1.svc If this is a Windows (R)
Communication Foundation service to which you have access, please check that you have enabled
metadata publishing at the specified address. For help enabling metadata publishing, please
refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata
Exchange Error URI: http://localhost:50154/Service1.svc Metadata contains a reference that
cannot be resolved: 'http://localhost:50154/Service1.svc'. There was no endpoint listening at
http://localhost:50154/Service1.svc that could accept the message. This is often caused by an
incorrect address or SOAP action. See InnerException, if present, for more details. The
remote server returned an error: (404) Not Found.HTTP GET Error URI:
http://localhost:50154/Service1.svc There was an error
downloading 'http://localhost:50154/Service1.svc'. The request failed with HTTP status 404:
Not Found.
It looks like having a custom ServiceHost/ServiceHostFactory might break simplified configuration. Is this the case, or is there something I'm overlooking which would make this continue to work?
If you override InitializeRuntime(), make sure you call base.InitializeRuntime() or you may see this behavior. After calling base.InitializeRuntime() before calling my additional startup logic, my service is now being correctly routed to.
I can inspect WCF messsages on both Client side and server side using IClientMessageInspector, IDispatchMessageInspector respectively. But in a Duplex comunications it is not clear how to do it in a callback from server to client (Nor much documentation on that topic).
Any ideas about how to implement this feature?
Finally I get the solution.
In a Duplex comunication scenario when a callback is made the server becomes the client and vice versa.
So on server side when implementing IServiceBehavior inject the message inspector using the CallbackClientRuntime property of the DispatchRuntime foreach EndpointDispatcher.
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher item in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher epd in item.Endpoints)
{
//injecting an inspector in normal call
epd.DispatchRuntime.MessageInspectors.Add(new MessageSizerInspector());
//injecting an inspector in callback
epd.DispatchRuntime.CallbackClientRuntime.MessageInspectors.Add(new MessageSizerInspector());
}
}
}
On client side when implementing IEndpointBehavior inject the message inspector using the CallbackDispatchRuntime.
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
//injecting an inspector in normal call
clientRuntime.MessageInspectors.Add(new MessageSizerInspector());
//injecting an inspector in callback
clientRuntime.CallbackDispatchRuntime.MessageInspectors.Add(new MessageSizerInspector());
}
Then apply the extension as always.
In my case I created a class like the following pseudo code
public class MessageSizer : Attribute, IServiceBehavior, IEndpointBehavior
{
.....
}
then I applied this attribute to service implementation for the server side inspection
and added a behaviorExtensions inside the app.config to setup the endpoint for message inspection on client side.
<system.serviceModel>
...........
<client>
<endpoint address="net.tcp://localhost/MinerDual.svc"
binding="netTcpBinding" bindingConfiguration="wsDualMinerNetTcp"
contract="WebApplication.IMinerDual" name="NetTcpMinerDual"
behaviorConfiguration="Default" />
</client>
<behaviors>
<endpointBehaviors >
<behavior name="Default">
<messageSizer/>
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="messageSizer"
type="WCFExtensions.MessageSizerElement, WCFExtensions,
Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</behaviorExtensions>
</extensions>
</system.serviceModel>
I'm working on my first WCF service, which will support several Ajax calls. I have an endpoint configured this way:
<service behaviorConfiguration="ServiceBehavior" name="AQM">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" bindingConfiguration="Binding1" contract="IAQM" />
</service>
and my behavior configuration:
<endpointBehaviors>
<behavior name="web">
<webHttp />
<enableWebScript />
</behavior>
</endpointBehaviors>
I need to create my own error handling so that I can format some specific information back to the client (see here http://zamd.net/2008/07/08/error-handling-with-webhttpbinding-for-ajaxjson/). My WebServiceHostFactory looks like this:
public class MyServiceFactory : WebServiceHostFactory
{
public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{
var sh = new ServiceHost(typeof(AQM), baseAddresses);
sh.Description.Endpoints[0].Behaviors.Add(new WebHttpBehaviorEx());
return sh;
}
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return base.CreateServiceHost(serviceType, baseAddresses);
}
}
public class WebHttpBehaviorEx : WebHttpBehavior
{
protected override void AddServerErrorHandlers(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
// Clear default error handlers
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Clear();
// Add our own error handler
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandlerEx());
}
However, after I created my own error handler, it seems it overrides the "enableWebScript" setting I had in my config above, which I think makes sense because now I'm creating my very own behavior dynamically which doesn't have any of the config settings above.
I read that this setting should be used with WCF/Ajax for security purposes (see here http://www.asp.net/ajaxlibrary/Using%20JSON%20Syntax%20with%20Ajax.ashx). So my question is, how can I set the the "enableWebScript" setting on my dynamically created behavior? Or is it not possible?
Update (6/1/2011): I'm also looking to dynamically change the behavior to use Windows credentials for authentication. In the config file it's done like this:
<bindings>
<webHttpBinding>
<binding name="Binding1">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Windows" />
</security>
</binding>
</webHttpBinding>
</bindings>
This is another setting I need to make programmatically since it seems to ignore the config setting.
For me it worked after adding the following constructor in WebHttpBehaviorEx
public WebHttpBehaviorEx()
{
DefaultBodyStyle = WebMessageBodyStyle.Wrapped;
DefaultOutgoingRequestFormat = WebMessageFormat.Json;
DefaultOutgoingResponseFormat = WebMessageFormat.Json;
}
There is a class WebScriptEnablingBehavior that you should be able to create in instance of programmatically and add it to the Behaviors collection of your endpoint. I've never tried it, and don't know how exactly that would work having multiple behaviors defined on a single endpoint, but I think that's basically what you're doing in your declarative configuration. Unfortunately WebScriptEnablingBehavior (which inherits from WebHttpBehavior) is sealed, so you can't just inherit from it.
Update: (from here)
The WebScriptEnablingBehavior is a "profile" of the WebHttpBehavior functionality designed specifically for interop with ASP.NET AJAX clients. It adds in some AJAX-isms like the ability to automatically generate ASP.NET AJAX client proxies.
I'm not sure you actually need to use <enableWebScript/>, like Carlos said, it sounds like it's only needed when you're using ASP.NET AJAX.
I am exposing a WCF service on SharePoint 2010 using a Service Factory class and cannot completly get rid of the tempuri.org namespace in the generated WSDL.
Here is what I do:
The svc file in the ISAPI folder
<%#ServiceHost
Language="C#"
Factory="Microsoft.SharePoint.Client.Services.MultipleBaseAddressBasicHttpBindingServiceHostFactory, Microsoft.SharePoint.Client.ServerRuntime, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"
Service="MyService, [...]"
%>
The service contract
using System.ServiceModel;
namespace MyService
{
[ServiceContract(Namespace="http://myname.com")]
interface IMyService
{
[OperationContract]
string GetSomeDefinition();
}
}
The service implementation
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using Microsoft.SharePoint.Client.Services;
namespace MyService
{
[ServiceBehavior(Namespace = "http://myname.com")]
[BasicHttpBindingServiceMetadataExchangeEndpointAttribute]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
class MyService : IMyService
{
public string GetSomeDefinition()
{
// do some stuff...
return result;
}
}
}
The WSDL
<wsdl:definitions name="MyService" targetNamespace="http://myname.com" [...]>
<wsdl:import namespace="http://tempuri.org/" location="http://localhost/_vti_bin/myservice.svc/mex?wsdl=wsdl0" />
[...]
</wsdl:definitions>
The problem now is that the WSDL is split up in two. One with the correct new namespace http://myname.com and one with the default namespace http://tempuri.org. Normally you get rid of it by using a bindingNamespace attribute on the endpoint configuration. But because I am using a Service Factory I cannot do so. Trying to define a service endpoint in web.config fails with the following error: A binding instance has already been associated to listen http://localhost/...
Web.config chunk
<system.serviceModel>
<services>
<service name="MyService.MyService">
<endpoint address="http://localhost/_vti_bin/myservice.svc" binding="basicHttpBinding" contract="MyService.IMyService" bindingNamespace="http://myname.com" />
</service>
</services>
</system.serviceModel>
2010-10-07 Update:
I have tried to derive the MultipleBaseAddressBasicHttpBindingServiceHostFactory and add the endpoint binding namespace when the ServiceHost is created. This does not succeed because the endpoint collection is empty at that point of time.
In general, there are three places you'll need to explicitly set the namespace to get rid of the tempuri.org default:
ServiceContract attribute (on contract)
ServiceBehavior attribute (on implementation)
bindingNamespace on relevant service <endpoint /> elements in the configuration file.
--larsw
I faced the exactly same problem and managed to resolve it thanks to this blogpost from Cameron Verhelst : http://cameron-verhelst.be/blog/2014/10/19/hosting-a-wcf-service-in-sharepoint-with-a-spcontext/ (Many thanks to him!)
The consumer of my service did not want the WSDL to be multipart so i had to correctly configure my service namespaces.
The key is to create his own service host and service host factory to modify the automatically created endpoints by the service host, and to reference it in the svc file.
Here is mine:
CustomMultipleBaseAddressBasicHttpBindingServiceHostFactory.cs
using System;
using System.ServiceModel;
using Microsoft.SharePoint.Client.Services;
public class CustomMultipleBaseAddressBasicHttpBindingServiceHostFactory : MultipleBaseAddressBasicHttpBindingServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
return new CustomMultipleBaseAddressBasicHttpBindingServiceHost(serviceType, baseAddresses);
}
}
CustomMultipleBaseAddressBasicHttpBindingServiceHostFactory.cs
using System;
using System.Linq;
using System.ServiceModel.Description;
using Microsoft.SharePoint.Client.Services;
public class CustomMultipleBaseAddressBasicHttpBindingServiceHost : MultipleBaseAddressBasicHttpBindingServiceHost
{
public CustomMultipleBaseAddressBasicHttpBindingServiceHost(Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
}
protected override void OnOpening()
{
base.OnOpening();
string targetNamespace = ImplementedContracts.First().Value.Namespace;
foreach (ServiceEndpoint endpoint in Description.Endpoints)
{
endpoint.Binding.Namespace = targetNamespace;
}
}
}
This service host allows to force the target namespace of the endpoint to be the same as the specified contract.