Converting type from a WCF service
I'm sure this must be a pretty common problem. I have two WCF services, that basically expose standard access and admin access to one larger service.
There is a similar method Foo
on each service. Foo
takes an argument of type TemplateInfo
which is defined in my service.
On my client I have开发者_C百科 a reference to Client
and AdminClient
. Client.Foo(TemplateInfo)
expects Client.TemplateInfo
while AdminClient.Foo(TemplateInfo)
expects AdminClient.TemplateInfo
. I know that once the service is reached Client.TemplateInfo
and AdminClient.TemplateInfo
are the same thing.
I'm trying to figure out how to instantiate TemplateInfo and/or cast it to work with both client and admin methods. There is a decent amount of code to create this object so I was hoping not to duplicate that for each type.
The types are defined in shared assemblies and I am "Reusing types" in my proxy. But I get the two different types because I have two service references.
It would be great to do something like (AdminClient)TemplateInfo, but perhaps I'll create a clone method to convert the types.
There's no way to do that. They are different types.
You can put that type into a class library that is references by both services and by the client. You can tell "Add Service Reference" to reuse types (it's the default), and then it will be the type from the class library that is used.
Note that the client application will also need to reference the same class library as the two services.
Either than fully generate a service reference, you can put types of the service in a shared assembly between client and service, and check "Reuse types in referenced assemblies" or something like this while generating the client class.
You can do it but it is generally not considered a good idea. The reason being that it tightly couples together your client and services. Only consider doing this if you are in control of the client and both services and are prepared for the fact that when you roll out a change to one of the services you could break the client.
Richard has beat me to it with some hints on how to do this if you are using visual studio.
But
One of the main principles of service oriented architecture is "Services share schema and contract, not class".
There are many good articles explaining this better than I can. Wikipedia is a good start or there is this article from way back when WCF had a cool name. http://msdn.microsoft.com/en-us/magazine/cc164026.aspx
精彩评论