The solution consists of two projects:
The DemoService project, which is a simple WCF service library that implements the IGetHeaders interface. This interface consists of a single method(GetHeaders) that retrieves some information about the headers in the message sent to the service. For this exercise, it returns the Action header.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Channels;
namespace DemoService
{
public class HeaderService : IGetHeaders
{
public string GetHeaders()
{
return OperationContext.Current.RequestContext.RequestMessage.Headers.Action;
}
}
}
The TestClient project, which is a console application that enables you to consume the DemoService service. A proxy to the DemoService has already been created.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestClient
{
class Program
{
static void Main(string[] args)
{
DemoService.GetHeadersClient proxy = new DemoService.GetHeadersClient("TcpIGetHeaders");
Console.WriteLine("And the header is: " + proxy.GetHeaders());
Console.ReadLine();
}
}
}
In the constructor for the object, pass the name of the binding to use as the sole parameter.
The app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WsIGetHeaders" />
</wsHttpBinding>
<netTcpBinding>
<binding name="TcpIGetHeaders" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8731/Design_Time_Addresses/DemoService/HeaderService/"
binding="wsHttpBinding" bindingConfiguration="WsIGetHeaders"
contract="DemoService.IGetHeaders" name="WsIGetHeaders">
</endpoint>
<endpoint address="net.tcp://localhost:8731/Design_Time_Addresses/DemoService/HeaderService/"
binding="netTcpBinding" bindingConfiguration="TcpIGetHeaders"
contract="DemoService.IGetHeaders" name="TcpIGetHeaders">
</endpoint>
</client>
</system.serviceModel>
</configuration>
My two questions:
In the service code, there is no constructor. Why in the proxy object, we can pass a sole parameter.
Why the parameter is the name must be the name of the endpoint, here it is ="TcpIGetHeaders".
The service does have a constructor - since one is not explicitly defined, a default parameterless constructor is supplied by the compiler. The following line of code will create a new instance of the service, even though there is no explicit constructor defined in it:
HeaderService myService = new HeaderService();
The proxy object is not creating an instance of the service - it's creating an object that can communicate with the service. When you add a service reference, .NET generates the code to create the proxy, as well as the call the methods exposed by the service.
If you look in the reference.cs file, you'll see the auto-generated code for the proxy. This proxy inherits from ClientBase<T> and implements the interface that defines your service.
DemoService.GetHeadersClient proxy = new DemoService.GetHeadersClient("TcpIGetHeaders");
The above code is calling the auto-generated GetHeadersClient class constructor, not your service constructor. The constructor for ClientBase<T> has several overloads - in the case of the code above, it's using the constructor that takes the specified endpoint configuration (determined by the name attribute on the endpoint element in the config file).
If you open the refernce.cs file, you'll probably see code similar to this (using an older example so the version numbers are probably different in your case, and I'm making an educated guess on the namespaces):
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class HeaderServiceClient : System.ServiceModel.ClientBase<TestService.DemoService.IGetHeaders>, TestService.DemoService.IGetHeaders
{
public HeaderServiceClient()
{
}
public HeaderServiceClient(string endpointConfigurationName) :
base(endpointConfigurationName)
{
}
public HeaderServiceClient(string endpointConfigurationName, string remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public HeaderServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress) :
base(endpointConfigurationName, remoteAddress)
{
}
public HeaderServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
public string GetHeadesr()
{
return base.Channel.GetData(value);
}
}
Notice how the constructors that have parameters reference the base constructor from ClientBase<T>.
In summary, you're calling the constructor for the proxy, and you're using the overload that takes a string for the endpoint configuration.
See ClientBase(T Channel) Constructor for more information.
Related
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.
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 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.
I am developing WCF services with basicHttpBinding, these services should be accessible using .net 1.1 & .net 2.0, for this purpose I am using basicHttpBinding. In old ASMX web services I assed one Soap Header (AuthHeader) to authenticate the user every request.How Can I authenticate in WCF using basicHttpBinding? Any sample Or tutorial will helpfull.
nRk
You can use AuthHeader as you did before switching to WCF. Maybe it will be more convinient for you, cause the princples will remain the same.
The bad thing i see in this solution is a plain text password transfer. Anyway, it's just another option and you can encrypt/decrypt the password somehow.
In this case you should implement your own your IDispatchMessageInspector & IClientMessageInspector, like
[AttributeUsage(AttributeTargets.Class)]
public class CredentialsExtractorBehaviorAttribute : Attribute, IContractBehavior, IDispatchMessageInspector
{
#region IContractBehavior implementation.
public void ApplyDispatchBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint,
DispatchRuntime dispatchRuntime)
{
dispatchRuntime.MessageInspectors.Add(this);
}
... empty interface methods impl skipped ...
#endregion
#region IDispatchMessageInspector implementation.
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
int i = request.Headers.FindHeader("username", "sec");
if (-1 != i)
{
string username = request.Headers.GetHeader<string>("username", "sec");
... do smth ...
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
return;
}
#endregion
}
In a sample i placed to header only username, but you can implement your a class containing username and password and use it instead of string.
On the client:
internal class CredentialsInserter : IContractBehavior, IClientMessageInspector
{
private string m_username;
public CredentialsInserter(string username)
{
m_username = username;
}
#region IContractBehavior implementation.
... empty interface methods impl skipped ...
public void ApplyClientBehavior(ContractDescription contractDescription, ServiceEndpoint endpoint,
ClientRuntime clientRuntime)
{
clientRuntime.MessageInspectors.Add(this);
}
#endregion
#region IClientMessageInspector implementation.
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
MessageHeader<string> mh = new MessageHeader<string>(m_username);
request.Headers.Add(mh.GetUntypedHeader("username", "sec"));
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
return;
}
#endregion
}
Then you should place attribute CredentialsExtractorBehaviorAttribute on your service implementation class.
[CredentialsExtractorBehavior]
public class DummyService : IDummyService
{
... impl ...
}
And on the client side you should do the following:
using (DummyServiceClient c = new DummyServiceClient("TcpEndpoint"))
{
c.ChannelFactory.Endpoint.Contract.Behaviors.Add(
new CredentialsInserter("_username_"));
c.DummyMethod();
}
First of all - yes you can! It depends on whether you use Transport or Message binding - if you're internet-facing, you're more likely to use message-based security.
Unfortunately, for message-based security, basicHttpBinding only supports certificates which is a bit of a pain.
wsHttpBinding on the other hand would support username/password or other methods as well.
You'd configure wsHttpBinding with username/password client credentials over message-based security like this:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="wsUserName">
<security mode="Message">
<message clientCredentialType="UserName"/>
</security>
</binding>
</wsHttpBinding>
</bindings>
<services>
<service name="yourservice">
<endpoint name="YourEndpoint"
address=""
binding="wsHttpBinding"
bindingConfiguration="wsUserName"
contract="IYourService" />
</service>
</services>
</system.serviceModel>
The section under <bindings> defines a binding configuration for wsHttpBinding that uses message-security with username/password client credentials.
The section under <service> defines a sample service that uses wsHttpBinding and that references that binding configuration that we just defined.
On the server side, you could now use the username/password that's being sent over the wire to validate your callers either in your Active Directory (everyone calling needs an AD account with you), or in the ASP.NET membership system database; or if you really really must, you could write your own authentication mechanism, too.
Find a lot of useful information on WCF security at Codeplex - excellent resource.
Check the scenarios here to try to match one to your situation. Each scenario is provided with a chceklist of items required to implement the solution.
I am relatively new to WCF. However, I need to create a service that exposes data to both Silverlight and AJAX client applications. In an attempt to accomplish this, I have created the following service to serve as a proof of concept:
[ServiceContract(Namespace="urn:MyCompany.MyProject.Services")]
public interface IJsonService
{
[OperationContract]
[WebInvoke(Method = "GET",
RequestFormat=WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
List<String> JsonFindNames();
}
[ServiceContract(Namespace="urn:MyCompany.MyProject.Services")]
public interface IWsService
{
[OperationContract(Name="FindNames")]
List<String> WsFindNames();
}
[ServiceBehavior(Name="myService", Namespace="urn:MyCompany.MyProject.Services")]
public class myService : IJsonService, IWsService
{
public List<String> JsonFindNames()
{ return FindNames(); }
public List<String> WsFindNames()
{ return FindNames(name); }
public List<string> FindNames()
{
List<string> names = List<string>();
names.Add("Alan");
names.Add("Bill");
return results;
}
}
When I try to access this service, I receive the following error:
The contract name 'myService' could not be found in the list of contracts implemented by the service 'myService'.
What is the cause of this? How do I fix this?
Thank you
Your contract is the Interface not the implementation.
Somewhere in the config you have written myService instead of IJsonService.
Remove the namespace from Service name. It will work fine.
Modify your web.config
You can find <services> tag and below of this tag you have to have two other tag :
<service ....
And
<endpoint ....
In <endpoint> tag you have to reference to interface of your class.
For exampl : If your service class named CustomerSearch and your interface named ICustomerSearch you have to config like this :
<service name="CustomerSearch" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="ICustomerSearch"
behaviorConfiguration="ServiceAspNetAjaxBehavior">
I had the same issue, but my solution was that in my web.config, I was specifying the entire class name (including namespace), whereas WCF would only accept a class name.
This didn't work:
<services>
<service name="BusinessServices.Web.RfsProcessor">
This worked:
<services>
<service name="RfsProcessor">
I have had that error before for ServiceModel framework 3.5, and I checked my host's config file. I found it was my cut-and-paste error. My service was pointing to an old non-existing service than the one I am using. It starts working again after I corrected these lines like below:
<system.serviceModel>
<services>
<!--<service name="NotUsed.Serv">-->
<service name="InUse.MyService">
<host>
<baseAddresses>
<!--<add baseAddress="http://localhost:8181/LastService" />-->
<add baseAddress="http://localhost:8181/InUseService"/>
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
Note that MyService has to be the name of your contract class in ServiceModel 3.5 BUT IT IS IMyService contract interface in Framework 4.0 -->
namespace InUse {
[ServiceContract]
public interface IMyService
{
[WebGet(UriTemplate = "/GetList/{PATTERN}",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
List<string> GetIDListByPattern(string PATTERN);
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class MyService : IMyService
{
List<string> MySample = (new _PointRT()).Sample().Select(r=>r._pointXID).ToList();
public List<string> GetIDListByPattern(string PATTERN) {
return MySample.Where(x => x.Contains(PATTERN)).ToList();
}
}
In the web.config file, the <service element's name attribute needs to be the service type's name with the namespace, but not the assembly (Namespace1.Namespace2.Class). The <endpoint element's contract attribute similarly has namespace-qualified interface type - Namespace1.Namespace2.Interface.
This also solves all behavior shenanigans, like CreateBehavior not being invokes on BehaviorExtensionElement.