WCF Service Reference name comes out as contract's name (interface), instead of the actual service name...? - wcf

Allow me to explain a little better.
I have a VS2010 Solution, split into 2 projects: a WCF Web Service, and a Class Library that uses the Web Service.
The WCF Web Service is a simple logging service; namespace "LogService", service "LogService", contract "ILogService".
When you use the "Add Service Reference" utility that comes with VS2010 to add a reference to the WCF, into the consuming Class Library project, you will see that the tool correctly adds a reference to a Web Service that can be called through "LogService.LogServiceClient". No problem here.
Now, I have another Solution; this is also split into 2 projects: another WCF Web Service, and a Web Application that uses this new Web Service.
This particular Web Service provides operations performed on a database; namespace "BLService", service "BLService", contract "IBLService".
Using that very same "Add Service Reference" utility in this project, however, adds a reference whose client name is actually "BLService.IBLServiceClient". Please notice there is a "I" prepended to the service client class name. The utility seems to have decided it should use the contract's name, instead of the service's name, to create the service client class.
I don't understand why does the tool behave differently on both Solutions. I've tried checking everything I could from both, and I can't find no significative differences (to my knowledge, at least).
Do you people know why does this happen? What things should I check, and correct, in my Web project (or anywhere else in the solution) so this odd behavior does not happen anymore?

Related

WCF - Application is using client's app.config rather than WCF service's app.config

In my VS solution, I created a console project, which I'm using as the client, and an empty project for the WCF service.
I then created the WCF service (created my contract and service type and manually constructed the app.config), and added a reference to the WCF service project in the client project.
However, when I called ServiceHost.Open() in the client, the endpoints weren't loading. I eventually determined that I needed to put all of the config information in the Client's app.config, rather than the service's app.config.
I'm not sure if this is normal, or if I'm doing something wrong. In the past, when I've used the WCF project template, this wasn't the case.
Yes, that is normal. Each .NET application (client, service, web site, etc.) has it's own configuration file. To be precise, there's a hierarchy of them, but the bottom end of that hierarchy is unique to the application.
This makes sense if you think about it -- the client would need to contact the service to ask for it's configuration, but it needs to know the endpoint information in order to even try to do that. So yes, the normal process is that client and service both have very similar information in their configuration files.
If you use Visual Studio's built-in tooling to do everything for you, it will automatically create and/or edit the configuration file for your client when you add a Service Reference to the project, copying from the metadata endpoint that WCF exposes for that purpose. Alternatively, you can use the WCF configuration editor tools to edit your client application.
Also, note that nothing actually enforces that your client and server have compatible settings; e.g. you can change the maximum sizes of many buffers/graphs/etc on one side, and not the other, and see some strange behavior. It's up to you to make sure both ends are working with mutually usable settings.

Is it possible to use "add service reference" option to add a self-hosted service in WCF?

I am creating a WCF service. At first I had one WCF service library project and one console project which was referencing the this library. And it worked fine when I used "add service reference" in my client project, as long as the service library had a valid App.Config file.
However, now I want to combine the service library and the console host into one console project. After all, the previous console project was pretty simple. But here comes the problem: when I choose "add service reference" and give the endpoint I defined in the App.config file of the new console project, it can't find any service reference. I take it because the service will not be hostedd by wcfsvchost.exe when the add service reference function was on(and I think it makes sense). But I can't think of a way to make it work. So how am I supposed to add a service reference, if my service is one single console-hosted project?
The easiest way for you is to run WCF service, generate a service reference and then combine it into single application. All you will need to do is just change address in your app.config. But be careful with this, as you should change address for client part and not server part of app.config.
This approach, however, will be difficult to maintain in future, when you will decide to change your service data contract and regenerate service reference. You might want to consider the following: wrap service reference into assembly and use assembly instead. This way you will be able to regenerate you service reference whenever needed.

Different method of consuming WCF

I have a WCF deployed on IIS. Now by adding web reference of it i am using it on my app.
So I have two questions:
Is it the best method of consuming WCF.
If the answer of first question is yes then what is the role of svcutil.exc, I mean what is the use of creating wcf proxy class. and if the answer is "No" then why?
It is the easiest solution if you develop with visual studio and have access the remote WCF service.
If you are developing using another IDE, you might want to use SvcUtil to generate your proxies.
If you prefer to have a simple CS file containing the generated client, you might also choose to generate it using SvcUtil.
You may also completely ignore SvcUtil and the Service Reference wizard and use the ChannelFactory class to generate proxies dynamically.
You should use "Add Service Reference" in Visual Studio (not Add Web Reference) for WCF.
It is the easiest way - since you can do it right in Visual Studio. What it does under the covers is basically call svcutil.exe (or you can do that manually, from the command line yourself), and create a service proxy class for use on the client side.
The use of svcutil.exe is many fold - you can create a client proxy class from a running service (or from an existing WSDL/XSD file), you can verify services, you can export metadata from a service for clients to consume, and a great many more options. It's the "Swiss Army Knife" of WCF tools.
WCF uses a concept that all calls to your service must go through a client proxy - this is the place where the entire WCF runtime lives, and where all the WCF extensibility points are located. This proxy converts your call to a method on the client into a serialized message that gets sent across the network to the server for processing, and it also handles "unpacking" the response from the call back into classes and objects on your client side for your use.
Adding a service reference is the quickest and easiest way but not always the best way. If you want performance then using ChannelFactory<T> is the way to go. You should know different ways to create a client side proxy and customisations that you can do.
An excellent resource is WcfGuidanceForWpf. Don't let the WPF in it scare you as it is really an excellent guidance for general WCF as well.

WCF Service Design

We currently have a WCF Service which is beginning to reach it's limits performance wise.
We have decided to add another server which will host another instance of the WCF Service.
We have web applications which must communicate with a specific server based on context... e.g. If the web application is dealing with objects from ServiceInstance1 then requests must be directed to ServiceInstance1's EndPoint. If the web application is dealing with objects from ServiceInstance2 then requests must be directed to ServiceInstance2's EndPoint.
I initially thought that a "Intermediate Service" or "Service Manager" could be created, the web application's Service Reference would be updated from the individual Service Instance to the "Intermediate Service" or "Service Manager" and said service would act as a "Broker" to the various Service Instances.
How is this accomplished?
I have currently added a ServiceReference to each service from the Manager however it seems that once a Service is "Referenced" it's types becomes specific to the that of the ServiceReference e.g.
ServiceInstance1's type's are all {ServiceInstance1}.
ServiceInstance2's type's are all {ServiceInstance2}.
I need the types to be the same on the web application end, so this obviously seems like the wrong way to do it.
I would also like that when methods are called on the client generated from referencing the "Intermediate Service" or "Service Manager" that the correct Service Instance is invoked, e.g.
IServiceManager.GetProjectById( {GUID} ) ->
Comes Back to ServiceManager ->
Determines which host has the project and returns the ProjectObject from the correct ServiceInstance.
Where ProjectObject is a Type Defined in ServiceInstance1 and ServiceInstance2.
I think the original service needs to have some of the DLL's pulled out so they can be referenced from the web application side and ServiceManager and a GenericWCF Client can be made.
If I am right hooray for me If someone can point me in the right direction I would appreciate it. If I am wrong can someone please scold me and show me how this is properly done!
The way to solve your problem is to create shared assembly with types used by both services. Reference this assembly on the client consuming your services (manager) and when creating proxies by Add service reference mark Reuse types from referenced assemblies.
What you are building is very simple message router. In WCF 4.0 there is additional support for routing services so you should check those features before developing your own. For WCF 3.5 MSDN magazine contains articles about building message router - part 1, part 2.
Just to Answer this and close it I wound up utilizing the Routing Strategy in .Net 4.0 and custom client class which I modeled after the generated classes from the Proxy.
Before I had the custom client ready I used the auto generated client code and I derived a class from it which allow me to change which service it was connecting to. I determined which service via a property which was made available on all service objects which were serialized.
Long story short this is working 100% as expected including the ServiceManager which can even be bypassed on certain calls which we allow.
We even have the ability to move a project from server to server during run time!
Thanks to everyone who helped! (Especially myself for actually doing the work without being spoon fed)
The easiest way to accomplish what you're trying to do is to stop generating your proxies using the server-hosted URL of the service. Instead, generate your proxies from the *.xsd and *.wsdl locally, and merely change the URL of the endpoint. Alternatively, you can use ChannelFactory<T> to generate proxies on-the-fly, and reference your interface .dll on the client side.
Once you've done that, you can use any common webserver load balancing technique to balance the load between the servers.
Not to put too fine a point on it, but Visual Studio's "Service Reference" is not useful, and should not be used, for services you develop. It's useful only for services developed externally, whose URL and contracts are likely to NEVER change. I personally have never had occasion to use it. For your own services, you should probably be using ChannelFactory<T> or a class based on ClientBase<T> to work out the proxies.

Testing WCF service with Fitnesse, should I add WebReference?

I want to use Fitnesse to do a subsytem testing of a WCF service.
Now to test a WCF service should I add the 'WebReference', and to add the webreference I require to host the service somewhere?
I believe Fitnesse as a new consumer to the service and it should add the WebReference.
For WCF, you should use "Add Service Reference" in Visual Studio, or svcutil.exe on the command line.
You can either add the reference from a running service (and then it needs to be hosted somewhere, yes), or you can extract the metadata (the WSDL that describes the service operations and the XSD that describe the message structures; again, using svcutil.exe) to files and create your client side proxy from those files.
If you only want to test the actual service implementation (without the WCF plumbing in between), you could of course also just add a normal reference to the assembly where your service implementation lives (which you hopefully isolated into a class library!), instantiate the service class, and call the methods on it. Depends on what you really want to test here...
Marc