Consume Wcf Service In Class Library ((FULL))
I have a WCF service that is running on IIS 8.5. I consumed and Tested the service in a Windows forms project and in a console application project and works fine! But I am not able to consume the web service from a class library project in Visual Studio 2013.
consume wcf service in class library
But when I try to consume the WCF service in a Class Library Application I got the error: Could not find default endpoint element that references contract 'bindSignalR.bindSinalRService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
When you consume a Web service in a library application (wherever you going to use your library), you have to copy the endpoint and the binding section of the app.config file from your library to the (web/app).config in the proyect that you want to use your LIBRARY!!! (Please correct me if something is wrong).
Here is a common scenario: I have a client application, which consumes business logic written in a different class library project. The class library has a service reference to a WCF service through either Visual-Studio-generated proxy classes or ChannelFactory. When the service reference is added to the class library, Visual Studio generates a bunch of configuration under system.serviceModel section of the class library project's config file. During runtime, unfortunately, the application will not pick up the configuration from this file. Instead, it will try to pick the configuration from the config file of the client application such as the app.config or web.config. Obviously, this will throw an error, since the configuration is missing in that file. One workaround is to copy the system.serviceModel configuration from the class library's config file to the client application's config file. While this might look to be a an immediate solution, it can be a maintenance headache if you have multiple client applications that use the same business logic library, where you would essentially end up duplicating the same configuration in each client application.
In one of the projects that I had to recently work, I had 9 client applications, all of which consumed the same business logic layer. This business logic layer was in turn calling a WCF service. The first idea was to copy the WCF client configuration in every client application, but that obviously was identified to be a maintenance nightmare, so I had to find a way to centralize the configuration in a class library (the BLL) and consume that class library in the client application. I googled a bit, found some resources, but most of them discussed the concept partially, provided code samples partially, which when I sat to implement kept throwing some kind of error. So, I decided to write a simple-to-understand step-by-step guide that described everything in detail so that somebody else might find it helpful.
Fortunately, there is a way to solve this issue and let the WCF proxy pick the configuration from class library's config file. This way we can centralize the service configuration and proxy generation at a single place. I am going to demonstrate two methods here of how this can be achieved:
What we have done so far is that we have created a WCF service, which is going to run on the server side. We created a new small service named BookService in it. Now we are going to call this service from a class library on the client side, which is described below:
Now add a new Solution folder and name it "Client." We will be putting our client-side code in this folder. Right-click this folder and add a new class library project. Name the project as BusinessLogicLayer. This class will work as the business logic layer for our application. This is where we are going to add a reference to the WCF service that we created in the previous section.
Note: As mentioned above, I am demonstrating here two ways through which you can consume the WCF service in the class library. The first is by adding a service reference using the Visual Studio wizard, which will auto-generate a proxy class. The second is to manually create a proxy class. Here I am splitting the remaining portion of this article into two as I originally mentioned above in this article.
The error says the endpoint configuration was not found. This occurs because the TestApp console application tries to locate the WCF configuration inside the TestApp's app.config file. In fact, currently, the configuration lies in the class library's configuration file. Now, we are going to make the application read the configuration from the class library's configuration file so that multiple client applications can centralize the business logic and service configuration in a single place - the class library.
To achieve this, we are going to make some additions and modifications to the class library. First of all, let us rename the app.config file to BusinessLogicLayer.dll.config. Right-click the configuration file in the BusinessLogicLayer class library in solution explorer and rename it to BusinessLogicLayer.dll.config. On the properties window, change the Build Action setting to "Content" and Copy to Output Directory setting to "Copy Always". This will make sure that the config file is copied to the ouput directory (usually the bin folder) of the client application and make the config file available at runtime.
Add a reference to the System.Configuration assembly so that we will be able to read the configuration file from within the class library. Then, add a new class file to the same project and name it ServiceManger. Add the following code to it:
The above code is used to create a dynamic WCF service proxy, which will read the configuration from the class library's config file instead of the client application's config file. The CreateServiceClient method then will return the newly created client object. Note that we are not going to directly use the MyService proxy object we earlier created via Visual Studio service reference wizard any more. The service reference was added just to make all the supported WCF service operations and the IBooServiceChannel interface available to the client application and additionally make use of the intellisense at coding time.
Now let us look into the second method of consuming WCF services - implenting your own proxy class, but still using the centralized configuration. In this method, we do not add a service reference to the project via Visual Studio wizard in Solution Explorer at all. So we are going to delete the MyService reference in the BusinessLogicLayer class library project under Service References folder, but before doing that, copy everything in the BusinessLogicLayer.dll.config file to clipboard because deleting the service reference might also clear the serviceModel section from this file. Once the service reference is deleted, check if the settings got cleared from BusinessLogicLayer.dll.config as part of the removal of the service reference, and if it did, paste the already copied content to the BusinessLogicLayer.dll.config.
What this this code does is that it creates a proxy class, which inherits from the ClientBase of type IBookService. We also have implemented the interface so that it internally calls the exposed service methods that the service channel offers.
I am creating a class library which will have business logic and i want to call this from SQL Server 2005 DTS. What i want to know is how to consume a WCF service from this class library project and compile it in a dll. How to deal with app.config created when servie is refered in class library? Could somebody send me a link to any article which can help me to implement the same in my current project?
What about app.config, I need to make this class library as a dll and register it in SQL Server 2005 and call the methods of this class library in SQL Server 2005. As you know we can do that by adding [StoredProcedure] attribute on top of the methods residing in Class library. Since we can not use the app.config we may need to use customconfigpath and read the addrress, binding etc from it. What i need is a link to good article explaining this.
I need to use the developed class library in my sql server 2005 and call this dll methods from sql server dts or ssis. I know when you give reference of wcf service it creates an app.config, which has end points info, my concern is when you deploy the dlls, how it can read the values of app.config, as it wouldn't be part of dll. As we all know app.config is an xml and would not get compiled, it needs to be explicit, i would like to know how to read the values of xml file which is in a c:\ or d:\ and read the end points, address etc programatically. My thought is as we need to create a customconfig path of an custom xml file which has info. of address, binding info. etc in an end point and we need to read programatically using a "CustomChannelFactoryHelper" helper class. I need a sample code for this. I saw a god article
namespace ConsoleApplication class Program static void Main(string args) Configuration conf = ConfigurationManager.OpenExeConfiguration(Assembly.GetEntryAssembly().Location); ServiceModelSectionGroup svcmod = (ServiceModelSectionGroup)conf.GetSectionGroup("system.serviceModel"); foreach (ServiceElement service in svcmod.Services.Services) string binding = service.Endpoints.Binding; string contract = service.Endpoints.Contract;
If it is a windows service / Windows or console applicaton reading from the MyApplication.exe.config file is easy ( We can put whatever you told me), but in my senario, i am going to call this newly created class library (inside class library i call the third party WCF Service) in SQL Server 2005 DTS or SSIS. In that scenario you will not have any exe or anything. One option is create a custoconfig file in which i can put these end points info and open the file in class library and read the nodes and use them in custom ChannelFactory code and call the service. Is this the only option i have or am i missing something or am i completely wrong? I saw the below link, i am thinking of a step by step example if any, if not i will explore more and create on my own from starting basing on the below links.