I've written a custom binding class that inherits from CustomBinding.
My custom class overrides the BuildChannelFactory method and uses a custom ChannelFactory to create a custom channel.
I'm having difficulties using the custom binding class in the WCF client.
I am able to use my custom binding class if I configure it in code:
Binding myCustomBinding = new MyCustomBinding();
ChannelFactory<ICustomerService> factory =
new ChannelFactory<ICustomerService>(myCustomBinding,
new EndpointAddress("http://localhost:8001/MyWcfServices/CustomerService/"));
ICustomerService serviceProxy = factory.CreateChannel();
serviceProxy.GetData(5);
My problem is I don't know how to configure it in the App.config file.
Is it a customBinding element, or a bindingExtension element? Is it something else?
When you created your custom binding in code, did you also implement a "YourBindingElement" (deriving from StandardBindingElement) and a "YourBindingCollectionElement" (deriving from StandardBindingCollectionElement) along with it?
If so - use that to configure your custom binding, as if it were any other binding.
The first step is to register your binding in the app.config or web.config file in the extensions section of <system.serviceModel>
<extensions>
<bindingExtensions>
<add name="yourBindingName"
type="YourBinding.YourBindingCollectionElement, YourBindingAssembly" />
</bindingExtensions>
</extensions>
Now, your new binding is registered as a "normal" available binding in WCF. Specify your specifics in the bindings section as for other bindings, too:
<bindings>
<yourBinding>
<binding name="yourBindingConfig"
proxyAddress="...." useDefaultWebProxy="false" />
</yourBinding>
</bindings>
Specify other parameters here, as defined in your "...BindingElement" class.
Lastly, use your binding like a normal binding in your services and/or client sections in system.serviceModel:
<client>
<endpoint
address="....your url here......"
binding="yourBinding"
bindingConfiguration="yourBindingConfig"
contract="IYourContract"
name="YourClientEndpointName" />
</client>
I couldn't really find a lot of resources on how to write your own binding in code on the web - Microsoft has a WCF/WPF/WF sample kit which includes a few samples from which I basically learned enough to figure it out :-)
There's one really good article by Michele Leroux Bustamante on creating your custom bindings - part 2 of a series, but part 1 is not available publicly :-(
Here's a sample custom binding in code for which you can download the complete source code: ClearUserNameBinding.
Marc
If you want to use this custom binding via configuration, you must extend the BindingCollectionElement abstract base and define bindingExtensions element in web.config.
Related
I am new to WCF, I am facing concurrency related issue in my hosted wcf service (.net framework 4.0) on IIS 7 / Windows 2008 server. I did all the possibilities after googling but still not able to fix my problem. I have created and inventory service which uses Entity Framework to fetch data from SQL Server tables like ItemHeadMaster, ItemMaster etc.
I referenced this WCF in my custom user search control for searching purposes. All is running well when 2 concurrent user hit search control placed on ASP.Net page.
My code looks like this:
namespace HIS.STORESERVICES
{
[ServiceBehavior(ConcurrencyMode=ConcurrencyMode.Multiple)]
public class StoreMasterData : IStoreMasterData
{
public string GetAllItemHead(string strHospitalId)
{
using (DAL.ItemHeadMaster objItemHeadMasterDAL = new DAL.ItemHeadMaster())
{
List<STORE.MODEL.ItemHeadMaster> objItemHeamMasterList = new List<STORE.MODEL.ItemHeadMaster>();
objItemHeamMasterList = objItemHeadMasterDAL.GetAllItemHead(strHospitalId);
XmlSerializer Xml_Serializer = new XmlSerializer(objItemHeamMasterList.GetType());
StringWriter Writer = new StringWriter();
Xml_Serializer.Serialize(Writer, objItemHeamMasterList);
return Writer.ToString();
}
}
}
I did following after googling:
added in config but NO EFFECT
<system.net>
<connectionManagement>
<add address="*" maxconnection="100" />
</connectionManagement>
</system.net>`
Added in config but NO EFFECT instead it gets more slow..
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" />
<serviceThrottling maxConcurrentCalls="32"
maxConcurrentInstances="2147483647"
maxConcurrentSessions="20"/>
Please help
Before WCF, to construct a service for cross process communications between processes in the same host, or in the same LAN, or in the Internet, you have to hand-craft transportation layers and data serializations for target environments and specific protocols.
With WCF, you just need to focus on creating data models (DataContracts after being decorated by attributes) and operation models (OperationContracts), and .NET CLR will "create" most if not all needed transportation layers and data serializations at run time, according to the configuration defined by you or the system administration in the target environment.
The defects in your codes:
WCF typically uses DataContractSerializer, NOT Xmlserializer to serialize things, and you don't need to call it explicitly, since the runtime will do it.
For most applications, you don't need ServiceBehaviorAttribute explicitly. You must know WCF in depth before using those advantage config which is not for beginner. And I rarely used them.
Your service interface function should comfortably return complex type rather the serialized text. In 99.9% of cases, if you have explicit serialization codes in WCF programs, the whole design is very dirty if not entirely wrong.
There are plenty of tutorials of creating Hello World WCF projects, and VS has one for you when creating a new WCF application. After you got familiar with Hello World, you may have a look at http://www.codeproject.com/Articles/627240/WCF-for-the-Real-World-Not-Hello-World
BTW, WCF serialization is very fast, check http://webandlife.blogspot.com.au/2014/05/performances-of-deep-cloning-and.html
I create WCF service with Spring.NET framework. This service is math service
and provide some computations for client apps.
I have question about WCF service parallelization on multi-core server. For simple
example I have server with 20 cores.
First here is a simplified code.
//WS interface
public interface IMatlabService
{
List<ResultData> Calculate(byte [] data);
}
//WS class definition
[ServiceBehavior(Namespace = "http://server.com/MatlabService")]
public class MatlabService: IMatlabService
{
public IMatlabManager MatlabManager{get;set:}
//web metod for math computations
public List<ResultData> Calculate(byte [] data)
{
var result = new List<ResultData>();
//do math work in another thread
Task<List<ResultData>> task = Task.Factory.StartNew<List<ResultData>>(() =>
{
return MatlabManager.CalculateWithFiniteElementMethod(data);
});
result.AddRange(task.Result)
return result;
}
}
public interface IMatlabManager
{
List<ResultData> CalculateWithFiniteElementMethod(byte [] data);
}
public class MatlabManager : IMatlabManager
{
public List<ResultData> CalculateWithFiniteElementMethod(byte [] data)
{
// do some math work
}
}
With Spring.NET I configure web service and manager class as not singleton.
Spring.NET XML configuration is here.
Matlab manager configuration:
<object name="matlabManager"
type="MatlabManager"
singleton="false"/>
MatlabService configuration:
<object name="matlabService"
type="MatlabService"
singleton="false">
<property name="MatlabManager" ref="matlabManager"/>
</object>
WCF service configuration from web.config
<behavior name="Behavior1">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
<services>
<service name="matlabService"
behaviorConfiguration="Behavior1">
<endpoint address=""
binding="basicHttpBinding"
contract="IMatlabService"
bindingNamespace="http://server.com/MatlabService"/>
<endpoint contract="IMetadataExchange"
binding="mexHttpBinding"
address="mex"/>
</service>
</services>
SVC file.
<%# ServiceHost Language="C#" Debug="true" Service="MatlabServiceService" Factory="Spring.ServiceModel.Activation.ServiceHostFactory" %>
I believe for each client web metod call is created new instance of
MatlabService and WCF service work is do on new thread (WCF service
thread) and OS assign this thread to CPU core.
Or I am wrong and behavior create new service object per call I must
define in ServiceBehavior property InstanceContextMode?
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)]
In web method Calculate of MatlabService I use
System.Threading.Tasks for parallelization so math work is do in
another thread (Math thread).
For each call is created WCF service thread and in WCF service
thread is created Math thread.
I am not sure if this is true.
Maybe is needed allow multi-threading in WCF service in
ConcurrencyMode?
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall,ConcurrencyMode
= ConcurrencyMode.Multiple)]
I would like heard ideas how can parallelize web metod calls for multi core CPU.
I google it but not find any clear and usefull for me because I use Spring.NET framework for creation WCF service.
The default instance context mode is PerCall, so setting that explicitly is redundant.
Yes, you're creating an additional thread to do the math operations, but you're not gaining anything because you're blocking until the task is complete. In fact, it is less efficient because you have the overhead of creating and mangaging an additional thread.
Each web call is already serviced in its own thread.
The only place I can see to add additional parallel computing is within the implementation of:
MatlabManager.CalculateWithFiniteElementMethod(data)
However, that looks like a call to Matlab. If you can reimplement that method using parallel code, you might be able to eek out some performance gains.
Whatever you do, profiling is the key to understanding if you're actually making it faster. Remember - parallel doesn't always mean faster. There is overhead in synchronizing these operations and creating the threads for them.
I am new to WCF i am trying to implement WCF Session Management but i am not clear about how to implement the session in WCF
This is my CODE
<wsHttpBinding>
<binding name="wsHttpBinding">
<reliableSession enabled="true" />
</binding>
</wsHttpBinding>
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples",
SessionMode=SessionMode.Required)]
public interface IMyService
{
...
}
This is not working...session is not maintained in my project
Now i wanted to know whether am missing anything or whether i need to add anything else in client or server side???? or this alone is enough to implement the session in my project???
It will be of great help if someone provide some ideas,suggestions,or sample code for implementing my task...
When you implement your IMyService in a class and a client connects to your service every client gains a new instance of your class.
There is a little example, that might help you:
http://www.devx.com/architect/Article/40665
How your service will behave depends not just on the SessionMode specified for the ServiceContract, but also on the InstanceContextMode under which your service implementation runs (controlled by the InstanceContextMode property of the ServiceBehavior). There is a helpful table here which tells you what to expect with the various combinations of these settings.
If this doesn't help solve your problem, please explain more specifically what behaviour you are expecting and what you are seeing.
In my solution I have a Web application project and a Class library project that contains all the business logic and this also acts as a data access layer as I am using Entity Framework. This means that I have my edmx in this layer itself.
I have some 34 classes in this class library project and at an average 6 public methods in each class. These classes were getting called directly from the web application until now. No problems. Now I want to introduce the WCF Layer between the UI and the Business logic layer.
This means I will have to write wrapper methods for all my methods and expose them in a WCF Service. Does this mean that 34 * 6 = 204 methods (approximately) will appear in my service layer as Operation Contracts? As per OO, I think this is too large a class and so it feels wrong.
I know there is the Generic Service design pattern, but is there anything else that I am missing? Please advise.
You could try RIA services
http://www.silverlight.net/getstarted/riaservices/
What I'm using is this.
Create a WCF service
2.1. Point the SVC service to your implementation like:
<%# ServiceHost Language="C#" Debug="true" Service="BusinessLayer.Service" %>
BusinessLayer.Service is a class in your Class project. (reference in service is needed)
2.2. Point the service behavior to the contract:
<service behaviorConfiguration="ServiceBehavior" name="BusinessLayer.Service">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="basicHttpBinding" contract="BusinessLayer.IService">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
</service>
Edit the name (BusinessLayer.Service) and contract (Businesslayer.IService)
Create the contract interface BusinessLayer.IService (in your Class project):
namespace BusinessLayer
{
[ServiceContract]
public interface IService
{
[OperationContract]
void DoWork();
}
}
Modify the existing implementation which uses the interface (here is your existing code):
namespace BusinessLayer
{
public class Service:IService
{
Public void DoWork()
{
}
}
}
Why do you want to wrap the entire business logic layer in a WCF layer? I would look very closely at your reasons for this before jumping into this new approach. Do you have physical reasons that you simply can't get around like the business logic that accesses the database needing to be outside the DMZ? If so, ok. But if not, I'd think twice about going down this approach to start with.
Having said that, if you have no other choice, I'd avoid the monolithic WCF class that wraps every public method that your UI needs. First off, I'd introduce an interface on the web application side so that you can depend on abstracts in the UI rather than concrete implementations. Further, I'd look into using WCF REST services. You can use ServiceRoute's to avoid having to introduce any *.svc files. Then you can decorate the methods you want to expose with WebGet/WebInvoke attributes. This could potentially save a lot of coding.
Well,
We have a similar application but the number of classes is even higher. Your concern here is that you are reluctant to provide serialization (that is what is needed to pass objects by WCF) to core classes of your business logic server.
Provided you have a classical three-tier application where business logic server and a client access the same database. What you need to do is simply 1) ensure all your objects have a unique identification (this could be a string or Guid) and 2) pass object ID in all WCF calls. What that means is that you DO NOT expose any classes on WCF side.
This might be quite is safer since you have a web application.
It is wrong. Your services should not have much more than 20 operations. If you need exactly same operations you should create contract and service wrapper for each business class. This usually results in chatty interfaces which are not good solution for distributed scenario. In that case you should model your service layer as facade which compounds several calls into one.
I am specifically looking for an example which use a) WCF & REST. After a long googling, though I got some but they are beyond my understanding.
Could some one please give me a very simple example say "Hallow World" or Summation of 2 numbers which will give me a clear insight about how to write a server, and also how to consume the same from the client end.
Also if any good link that explains this kind of example in simple terms kindly tell me that.
Thanks
REST in WCF is not that hard once you figure it out.
First you must define your interface.
Here is an example.
[ServiceContract]
public interface IRESTExample
{
[WebGet(UriTemplate = "interaction/queue?s={site}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string QueueInteraction(string site);
[WebGet(UriTemplate = "interaction/cancel?id={interactionId}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string CancelInteraction(string interactionId);
[WebGet(UriTemplate = "queue/state?s={site}&q={queue}", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
string QueueState(string site, string queue);
}
You can see in the WebGet you define the final URL. So it depends on where you bind it, but say you bind the endpoint to www.example.com/rest
QueueInteraciton would be www.example.com/rest/interaction/queue?s=SomeSite
Where {stie} or {parameterName} is replaced with the name of the parameter.
The implemetion is just a simple class, I am going to assume you know how to implement an interface. If you need help just leave a comment.
Now binding the endpoint. In the end it is not that hard, you can do it all in the config.
<system.serviceModel>
<services>
<service name="Stackoverflow.Example.Service.RestExample" behaviorConfiguration="MyServiceTypeBehaviors">
<host>
<baseAddresses>
<add baseAddress="http://localhost:2136/RestExample"/>
</baseAddresses>
</host>
<endpoint address="rest" binding="webHttpBinding" behaviorConfiguration="xmlBehavior" contract="Stackoverflow.Example.Service.IRESTExample" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors" >
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonBehavior">
<webHttp/>
</behavior>
<behavior name="xmlBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name = "NoSecurity">
<security mode = "None" />
</binding>
</basicHttpBinding>
</bindings>
</system.serviceModel>
Now the code to start the service and bind it. YOu can do it in anything, for example a console app.
RestExample exampleService = new RestExample();
host = new ServiceHost(exampleService);
host.Open();
This should be enough to get started.
msdn article An Introduction To RESTful Services With WCF with code example at msdn code gallery. Also check out this codeproject article
David Basarab's response is correct, but there's a much simpler way to do this without all the manual wire-up. Especially if you've used to classic ASMX web services and don't have a lot of WCF experience, the following method is dirt simple.
In a Visual Studio 2010 web project, add a reference to System.ServiceModel.Web.
Chose "add new item" in your project. The template you want is in "Web" and is called "AJAX-enabled WCF Service". Don't choose the vanilla "WCF Service"! If you do you have to do all the web.config wiring yourself that David Basarab described, and it's a pain. The "AJAX-enabled WCF Service" does all that setup for you. Name your service whatever you want.
Open up the .svc file. In the [ServiceContract] attribute on your class, fill in the Namespace string parameter with whatever you want.
You'll get a sample DoWork() method and a bunch of comments telling you what to do in the file. The trick to getting the RESTful API is to add [WebGet()] attributes to your web methods. Add one to DoWork() and verify everything functions for you.
So, to call the DoWork() method, you'd hit this in your browser:
http://localhost/MyAjaxEnabledService.svc/DoWork
Let's add a new HelloWorld() method now that shows some parameters and output.
VB:
<OperationContract()>
<WebGet(ResponseFormat:=WebMessageFormat.Xml)>
Public Function HelloWorld(ByVal name As String) As String
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml"
Return String.Format("Hello, {0}!", If(String.IsNullOrWhiteSpace(name), "world", name))
End Function
C#:
[OperationContract()]
[WebGet(ResponseFormat=WebMessageFormat.Xml)]
public string HelloWorld(String name) {
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
Return String.Format("Hello, {0}!", String.IsNullOrWhiteSpace(name) ? "world" : name);
}
Now you can visit:
http://localhost/MyAjaxEnabledService.svc/HelloWorld?name=MattMc3
There's a lot of crappy and confusing documentation out there about WCF, especially for those who crave the simplicity of the old .ASMX style. Hopefully this helps someone get started with WCF. There's a lot more you can do with it than the old ASMX-style, but it's hard to ramp up and not get discouraged with MS for their poor help with the transition from ASMX. You can read more about quick-and-dirty RESTful WCF services here.
If you really want to do ReST then use a web framework that will lead you down the right path. See OpenRasta.
It is not impossible to do WCF to do ReST, it is just very difficult to learn how to do Rest with a framework that will frequently get in your way and lead you in the wrong direction.
In Microsoft Web Developer, you can use the Online Template "WCF REST Service." It will set up a project for you with the correct web.config and global.asax files.
You can create an WCF REST web service by configuring your endpoint to use a webHttpBinding as seen in this in-depth tutorial:
http://www.west-wind.com/weblog/posts/310747.aspx
Here is another open source web services framework that simplifies creating XML and JSON REST web services without requiring any extra configuration.
Edit: Added link to good article articulating the spirit of REST:
http://tomayko.com/writings/rest-to-my-wife
Link to blog post comment explaining common misconceptions of REST:
http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven