How to get the binding that a particular WCF service is using - c#

Is there a way to get the binding that a WCF service is using from the object instance of the service class itself?
I'm currently debugging a WCF service and have changed the client bindings and want to see if they are the one's that are being applied to the proxy class that represents the service interface.
As an aside the proxy class is being created using the ChannelFactory<IMyWCFService>.CreateChannel() method. However as this is a MVC web application and the method call is done via Dependency Injection. So the proxy object is injected into my code.
So just to be clear at the point in the code where I am using the proxy object I don't have access to original ChannelFactory. I know the binding that the ChannelFactory is using as it is set whilst configuring the dependency injection container within the Global.asax.cs class. However as we are using dependency injection there is no guarantee that the ChannelFactory or indeed binding defined in the Global.asax.cs is the one being used to create the proxy object as it might have been changed by some other code. The issue I am having is we are changing explicitly setting values on the binding used in the Global.asax.cs class but this settings aren't taking affect which makes suspicous that another binding is being used.

You can get binding from Endpoint object of service:
var service = new Service1Client();
Binding binding = service.Endpoint.Binding;
Console.WriteLine(binding);

Related

How to set WCF base addresses in code when using the Configure method

For pure code-based WCF configuration, in .NET 4.0 one could configure a base address for a service when creating an instance of a ServiceHost (assuming a self-hosted scenario, which is suitable for my needs). While one could still do this with .NET 4.5, MSDN implies that using the new Configure() method is a better practice because it makes such code-based configuration a bit easier for self-hosted and a lot easier for web hosted scenarios (see Configuring WCF Services in Code).
The problem, however, is that though the object you have to work with inside the Configure method (a System.ServiceModel.ServiceConfiguration) has a BaseAddresses property it is read-only. Surely there must be a way to set the base addresses in code? My web searches have turned up not only nothing about this specific question, but no articles or posts about this Configure method at all, except for the single MSDN page I have referenced!
Looking in reflector what BaseAddresses property does:
// System.ServiceModel.ServiceConfiguration
public ReadOnlyCollection<Uri> BaseAddresses
{
get
{
return this.host.BaseAddresses;
}
}
I was unable to find any other method in ServiceConfiguration working with ServiceHost.BaseAddresses
Instance of ServiceConfiguration is created using ServiceHost instance:
// System.ServiceModel.ServiceConfiguration
internal ServiceConfiguration(ServiceHost host)
{
ServiceConfiguration.CheckArgument<ServiceHost>(host, "host");
this.host = host;
}
This means that ServiceConfiguration is just an extension to standard way of configuring ServiceHost. So I would suggest any old way of setting BaseAddress is still considered as the best practice (programmatic or using configuration).

Consuming web service in class library

Scenario:
I am consuming a web service in my class library project and it generates a binding name and end point in app.config. If I reference the class library in my UI project, I also have to include the same configuration in web.config. My problem is I don't want to include this configuration in web.config because of the dependency. I want to use assembly as it own with out any dependency.
My solution approach:
When I create the instance of proxy class in the class library project it shows me constructor to pass binding and endpoint.
Example
wsProxy proxyClass = new wsProxy(System.ServiceModel.Channels.Binding binding, System.ServiceModel.Endpoint endpoint)
I was wondering if I can pass the same binding and endpoint that I have in app.config so that I don't have to include either in app.config and web.config.
Yes, you can create these classes without having matching configuration in the main .config file. Where you get that configuration is up to you; it could be App.config, a YML configuration file, a database, etc. As long as your code satisfies the constructor requirements for the classes you're instantiating, you'll be fine.
With WCF, everything defined in your configuration file can be done programmatically.
You just need to create the objects needed to instantiate your client. Depending on the WCF features you want your application to be leveraging, you'll need classes like EndpointAddress, AddressHeaderCollection, Uri, EndpointIdentity (DnsEndpointIdentity or SpnEndpointIdentity), Binding (WSHttpBinding, NetTcpBinding etc.). And you might want to have these objects populated from a decoupled, centralized configuration store such as a database.

Default property values for WCF client

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.

Retrieving data received by the server using WCF Web Services

I'd like to use a web service (created in my main thanks to new ServiceHost(typeof(..)) ) and I would like to retrieve data received by my server to process it in my main.
I've begun with a simple event handler but it is impossible because I have to create an instance instead of using typeof.
Is there another solution?
You can use a singleton WCF service. You need to decorate the service class with [ServiceBehavior] and specify InstanceContextMode.Single, and then you pass an instance of the service class to the ServiceHost constructor.

Init properties in my base wcf service

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)

Categories

Resources