I have a self hosted TCP based WCF service. I am now building a project that consumes that service, but there seems to be at least two ways of adding a service reference to a project and the both produce wildly different proxies. First I used the "Add service reference" from the project menu, but this generated quite a few files and even some XML schemas of the core .Net types. Then I tried the SvcUtil which only produced two files, one proxy and one config file that holds the service reference and binding parameters, this is much better but...
In both cases the VS tools seem to reproduce type definitions even though I have provided a reference to the assembly containing the service and all the types it uses. For example, some of my service methods return generic collections of Entity classes. All the Entity classes are defined within an assembly that I have directly referenced from the consuming project so why redefine those types again?
I would be grateful if some body could offer some advice on consuming WCF services that return Entity types and any best practices they follow.
We have found that the add service reference creates alot of unneeded code that gets in the way more than it helps.
We have gone over to a manual way of setting it up, there is an introduction to this method here:
http://perseus.franklins.net/dnrtvplayer/player.aspx?ShowNum=0103
Yes, in your concrete case this may seem like duplication - but consider this: WCF is also designed to be interoperable, and in MOST scenarios, especially if you have a non-.NET client calling your code, you won't have the assembly with the contract and the interfaces available.
So there's really nothing BUT creating a full proxy, that contains all that information, in order to work in all possible circumstances.
Now if you really want to avoid duplication of data contracts etc., you can compile those into their own assembly, and then use the /r:(assembly name) switch when calling svcutil to tell it to re-use the code and contracts in that assembly.
Marc
Related
It was my understanding that when a developer (a company) develops both client and service, it’s better to put data and service contracts into a separate assembly to be used by both client and service applications. It is to avoid code duplication while generating a proxy classes using e.g. svcutil.
Is this indeed the preferred approach and have you ever had a project that was an exception from this rule?
We do this all the time in our projects and i don't know what can be said against that approach.
Sharing contract assembly can lead to unwanted dependencies, since these contract classes such as datacontract\servicecontract can contain methods. These methods then can get called transparently in client\server code hence breaking the encapsulation of these contracts. Data\Service contracts are meant to be used only as an mechanism to share data.
As the subject line describes, I am in the process of exposing a C# library into a WCF Service. Eventually we want to expose all the functionality, but at present the scope is limited to a subset of the library API. One of the goals of this exercise is also to make sure that the WCF service uses a Request / Response message exchange pattern. So the interface /API will change as the existing library does not use this pattern
I have started off by implementing the Service Contracts and the Request/Response objects, but when it comes to designing the DataContracts, I am not sure which way to go.
I am split between going back and annotating the existing library classes with DataContract/DataMember attributes VS defining new classes which are like surrogate classes to the existing classes.
Does anyone have any experience with similar task or have any recommendations on which way works best ? I would like to point out that our team owns the existing library so do have the source code for it. Any pointers or best practices will be helpful
My recommendation is to use the Adapter pattern, which in this case basically means create brand new DataContracts and ServiceContracts. This will allow everything to vary independently, and will allow you to optimize the WCF stuff for WCF and the API stuff for the API (if that makes sense). The last thing you want is to go down the modification route and find that something just won't map right once you are almost done.
Starting from .NET 3.5 SP1 you no longer need to decorate objects that you want to expose with [DataContract]/[DataMember] attributes. All public properties will be automatically exposed. This being said personally I prefer to use special DTO objects that I expose and decorate with those attributes. I then use AutoMapper to map between the actual domain models and the objects I want to expose.
If you are going to continue to use the existing library but want to have control over what you expose as the web service API, I would recommend defining new classes as wrapper(s) around the library.
What I mean to say is don't "convert" the existing library even if you think you're not going to continue to use it in other contexts. If it has been tested and proven, then take advantage of that fact and wrap around it.
Just personal style, I guess, but I hate having 2 files for my WCF Services. I tend to like to copy/paste the interface into the .cs file so that I only have to deal with a single file.
Any dangers in doing this?
Not dangers per se - but there are times when it is very useful to have a separate assembly with your service, operation and data contracts (just the contracts, the interfaces, basically) - when you need to share those between the server and the client side.
There's really no point in sharing the whole service implementation code (the actual service class, that implements the service interface), with the client.
Plus: if you have your interfaces in a separate file (and possibly assembly), it makes it easier to write unit tests, especially if you want to mock a service. Gets a bit messy if you mix interface and class into a single file.
So I consider it a useful and helpful best practice to have separate files for interfaces and implementations (actually: always one class per file only), and to put all service- and data contracts (and fault contracts) into a separate assembly.
Actually, I like to go beyond two files, and have two separate projects. One project holds the interface definition. The primary value is for integration testing. I like to make a third project with a WCF client. That client accesses the interface in the "shared" assembly.
Remember the mantra of Testivus: "When writing the (production) code, think of the test; When writing the test, think of the code."
Please note that my experience in Silverlight/.Net and WCF is about two weeks of googling and deciphering tutorials. I need to attempt and provide feedback to a client on if Silverlight will be a possible solution to their application needing a RIA front end.
The client has a rather large .Net based application with a UI layer built which greatly relies on the creation and manipulation of specific (personal) classes and objects from the backend (which would be the server side).
A summery of what I understand to be the general procedure: one can pass simple objects containing simple data types, or more complex .Net type objects. Basically anything which can be understood by both client and server side, after serializing.
But what is the limitation to the complexity of an object I can pass? Or phrased otherwise, would silverlight and WCF be able to support the passing of a personalized object which may contain references to other classes/objects and variables etc?
Additional Info (in case it can help):
I am not allowed direct access to their backend code but with the information I have been given I can safely say their classes heavily use inheritance and overloading of functions/methods in the classes.
As far as I know there is nothing specific to Silverlight. There are some things to keep in mind though.
WCF serialization doesn´t like circular references.
All types need to specified in the contract. So watch out with inheritance etc.
In general using DTO's (Data Transfer Objects) and not exposing your business objects is the way to go.
The metaphor is one of message passing as opposed to passing objects. DTO's as Maurice said.
You can get pretty complex, but each object needs to have its contract defined.
WCF promotes good design by using interfaces and contracts etc. What baffles me is that, for example in my case if I have 2 sets of business functionality like ICustomerMgmtBIZ
and IProductMgmtBiz. If these two are ServiceContracts, and I have an interface like
IBusinessService:IProductMgmtBIZ,ICustomerMgmtBIZ
and implementation class BusinessService. I see that BusinessService class will be having too much implementation. The workaround I have been using so far is by implementing partial classes.
So bluntly put, can a WCF service have only 1 implementation and 1 service contract ??
No, it is possible to implement more than one Service contract on a WCF Service type (the class that is attributed with the ServiceBehavior attribute), since it is just a matter of having the class implement multiple interfaces. If you are using any of the Visual Studio templates or other kinds of code generators, this may not be immediately clear. However, although you can implement more than one Service Contract interface on a Service type, it does not do you much good if you need the service, presumably a singleton in this case(?), to behave as one service. IBusinessService implies that you need all of the service's functionality to be callable from one client proxy, so that all operations may operate in the same logical session (similar to ASPX web session). If that is not the case, then you are free to define individual proxies for each contract interface, but that will also require that you support one endpoint for each contract.
Is it an absolute requirement that you can only have on WCF ServiceHost instance for your implementation? What factors are influencing your decision?
By the way, partial classes do not trouble me anymore. The idea of splitting out code into multiple files now seems rather natural. For example, storing partial classes in files like ServiceType_IProductMgmtBiz.cs and ServiceType_ICustomerMgmtBIZ.cs seems natural enough, in addition to storing the core logic in ServiceType.cs.
Finally, the following question might be of use...
WCF and Interface Inheritance - Is this a terrible thing to do?
Bluntly put, no - sort of - yes, but. Any workaround is non-optimal and involves using an "IBlank" as a master WCF interface (where your interfaces derive from IBlank), and two endpoints, one implementing IProductMgmtBIZ and the other implementing ICustomerMgmtBIZ. I don't have my dev machine in front of me, this might involve some other overrides. So, at the WCF level you're screwed unless you want to have two WCF ServiceHosts (which is perfectly reasonable).
In short, the workaround is inelegant. Its easier to have two WCF endpoints on the same port with a different extension.