I have a WCF service hosted in a console application. I have two ways to create WebServiceHost, either I create the instance of class implements service contract, and use that instance to create WebServiceHost, or tell WebServiceHost the type name of class implements service contract and let WebServiceHost controls everything.
In the first way, InstanceContextMode doesn't have any effects, it always equals to InstanceContextMode.Single, correct? InstanceContextMode only works when I do the other way: tell WebServiceHost the type name and let it controls creation of service contract implementation class. Is my understanding correct?
Yes, this is correct. MSDN says this explicitly - instancing works differently when you pass explicit object.
Related
I have created a SOAP service.
Now i want to consume it in a c# client application. I added the service using 'Add service reference' and service reference is added to client.
All my service entities are in service. And in current scenerio i can't move them to a common library.
Problem is, my service endpoint is accepting List<Foo> as parameter.
Foo has a method Boo.
In client, when i try to Foo.Boo() i get Cannot resolve symbol Boo error.
Unfortunately only methods on the service itself are exposed via a SOAP web service, methods on objects used as parameters or return values are not. If the method relates to a server-side operation then you could expose it at the service root level taking the instance object as parameter, or if it relates to a client-side operation you could consider adding it as a client-side extension method.
I am a bit confused on what exactly I am instantiating with the WCF ServiceHost when I have multiple endpoint contracts that I am adding to it.
The instantiation has included a typeof argument - which seems to be the contract and is so in everything I have read and done. However when I come across adding additional contracts - this is where my confusion about it is.
ServiceHost shost = new ServiceHost(typeof(MyService), NetTcpBinding, xyz);
So let's say I have several contracts - ProductService, BatchService, CustomerService these are endpoint contracts that each have an interface. Let's keep it simple there is an Add Method and a Get Method in each of these contracts.
I can then add these endpoints which are contracts to the ServiceHost ..
shost.Endpoint.Add(ProductService);
shost.Endpoint.Add(BatchService);
shost.Endpoint.Add(CustomerService);
This is my confusion if I create it with MyService, then does MyService need to incorporate the methods of all of my endpoint contracts or does this just pass in the first Endpoint Contract just to instantiate it and then all the additional ones are (forgive my lack of a better way of saying this) - additional services provided by the service that was instantiated with one of my endpoints ?
I have read on SO and looking here seems relevant and close - but does not give an explanation of the instantiation of the ServiceHost
Run WCF ServiceHost with multiple contracts
I mean what is the point of instantiating the thing ; and then adding endpoints if you have to place all of the endpoint methods into the host contract anyway where btw you can specify namespaces for the contract as well..- that just seems so un oop .. is the answer found at the link really the viable answer (it smells WET ~ W'peat Every Thing - AKA not DRY].
The ServiceHost can host one service - that is one service class (implementation class). But that single class can implement multiple WCF service contracts.
So if you have three service contracts (as interfaces IProductService, IBatchService, ICustomerService) and a single class MyServiceClass which implements all three of those interface contracts
public class MyServiceClass : IBatchService, ICustomerService, IProductService
then you can host this class in a ServiceHost and you can define endpoints for each of those three service contracts.
I have the following scenario:
There is a complex object that is residing in the standalone class library and some of the object properties have default values.
The object is argument for WCF public method.
I instantiate this object on the client of WCF, assign values to properties and pass it to the WCF public method.
The WCF method on service side accepts it and does whatever.
My problem is that when I instantiate the object on the client property default values are not available for me to use and I have to assign them in code again.
I looked through the past questions on the topic here and did not find anything related to my scenario. I don't really have data contract for the argument although on the client my object gets instantiated not from the class library itself but from the service reference, like WCFServiceReference.MyClass (otherwise WCF method can not accept it as argument).
I would really like to have all those default values to be available on the client.
I would appreciate any assistance.
Thanks!
With your approach -- using the service proxy classes -- the client only gets the properties marked with DataMember. That's why the default values you assign don't appear in the client -- that code is not serialized, so it's not sent with the WCF service.
If you want to share code, you can do it by declaring your DataContract classes in a separate class library. Have the WCF service and the client both reference that library.
I have set up some WCF services that I use to call from javascript to enable ajax calls such as retrieving data in JSON format.
All my services are configured in a web.config etc and I code in c#.
All my services inherit from a BaseService object which has some common properties that are share between all services, for example a logger object, configuration object etc.
My question is, how/where I can set up/assign values to these properties? ie. set up the base service logger property with an instant of a custom logger I created. Apologies if my question is vague but any help would be great.
Properties for Service implementation object cannot be set from client (any kind) because only contract is visible to client. Further, properties such as config and logger are anyway not meant to be part of contract but rather service implementation - so they must get set at server side automatically(i.e. by application infrastructure) for each service object. You have a couple of choices:
Use constructor (either that of BaseService or actual service class) to initialize these properties.
Use IoC/DependencyInjection container (for example, Unity, StructureMap)
Is a web service reference the proxy class itself? Or is it the classes created inside that you see in object explorer when you look at your web service reference?
example, I created this web service reference
http://www.elbalazo.net/post/TestWebProject%5FObjectExplorer%5FWebReference.jpg
I assume ServiceAuthResponse is one proxy class inside my web service reference?
When you add the WebService reference a proxy class is generated for you.
In your example it looks like LitleWebService will be your service proxy, ServiceAuthResponse sounds more like a data contract that will be used by the service. If you read about the Proxy Design Pattern it may be of some interest
Normally you proxy will inherit from ClientBase, this is where you can specify the service contract.
public class MyProxy : ClientBase<IServiceContract>, IServiceContract