Relocating app.config to a custom path for WCF? - wcf

I need to load a custom app.config for WCF.
I've tried he solution entitled "Relocating app.config to a custom path", but unfortunately, this technique just won't work for WCF, it still throws an error saying it can't find the .config file (correct me if I'm wrong).

Thanks to WCF Client without app.config/web.config by Kishore Gorjala, I eliminated all reliance on an app.config as follows:
EndpointAddress endpointAddress = new EndpointAddress("http://myServiceURL.com");
WSHttpBinding serviceBinding = new WSHttpBinding();
serviceBinding.ReceiveTimeout = new TimeSpan(0, 0, 120);
MyServiceClient myClient = new MyServiceClient(serviceBinding, endpointAddress);
According to this blog, you might want to try BasicHttpBinding instead of the WSHttpBinding as well.
This technique is also mentioned on the blog Minimal WCF server/client WITHOUT app.config.
Experimental evidence: This worked perfectly - and no more app.exe.config to worry about.

As you can see from the question you referenced, this does not work. In fact, it doesn't work anywhere in .NET - the problem is not specific to WCF.
There is only a single config file per AppDomain. Period. You will always have to copy settings from the DLL's app.config into the single config file.

There is a method to load app.config from a custom location, see Loading the WCF configuration from different files on the client side by Pablo M. Cibraro (aka Cibrax).
This method relies on overriding CustomClientChannel with a constructor that loads a custom app.config, then using the following code to create the client:
CustomClientChannel<ICalculator> channel = new CustomClientChannel<ICalculator>("OtherConfig.config");
ICalculator client = channel.CreateChannel();
Download the Microsoft Visual Studio sample project here.
To fix the compile errors under Visual Studio 2010, upgrade both projects to .NET 4.0, and reload the five referenced assemblies it can't find. To add it into your project is simple: just add the extra class, and replace the original channel instantiation line with the new one that uses a custom app.config.

Related

Connection String not found in Config File

I am new to ADO.NET, I am using MYSQL with WCF.
I have a WCF project with ADO.NET, the SalesSolLIB project has reference of SalesSolWCF, and SalesSolView has reference of SalesSolLib project.
The problem is ADO.Net entities are created in WCF and when run, the error comes as although the name is present in web.config file.
"No connection string named 'SalesSolEntities' could be found in the application config file."
WebConfig File
<connectionStrings>
<add name="SalesSolEntities" connectionString="metadata=res://*/SalesSolDBModel.csdl|res://*/SalesSolDBModel.ssdl|res://*/SalesSolDBModel.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=surpavan;persist security info=True;database=salessol;old guids=True"" providerName="System.Data.EntityClient" />
</connectionStrings>
Code in WCF file:
Dim DB As New SalesSolEntities()
DB.Database.Connection.Open()
DB.Database.Connection.Close()
Return "test"
Code in SalesSolLib:
Dim service As New SalesSolWCF.ContactService
Dim errormsg As String = service.SaveContact(wcfcontact, IsNewContact)
However, through browsing, I found that connection string must be all the projects, however, why would I need to use WCF if connection string is needed in all projects, please advice how this can be solved, I don't want to put connection string in all projects or did I do wrong?
I am using Entity Framework 5.0 and the WCF Service Application as wcf project type.
You are calling your WCF services the wrong way. You do not add the reference by simply adding reference to DLL, you need to expose WCF endpoints via MEX, and then add reference by using "Add Service Reference".
What you've done now is that you're using your WCF not as a a service but just as a simple class that you make an instance off and then call it's methods. That is not how WCF services should be used, and that is why you get "connection string" missing error. Method in your class talks to database, so when you create instance of that class in other project, and call that method, that project must also have matching connection string. Basically it is all wrong, should not be done this way.
WCF services must be hosted either as self-hosted in your application or within IIS. I'd suggest that you go and take some basic WCF tutorial, otherwise what I just wrote will not make much of a sense to you.

How to turn a basic class library into a WCF Service Library

I was trying to copy the functionality of an existing WCF service library to use it as a template for a new WCF service library. Since I was going to cut and paste all the code that I needed, and I didn't want it to create the default service stubs, I didn't start it by using the built-in "WCF Service Library" project template. I just added a new blank project to my solution, using the "Empty Project" template.
I then created two new blank .cs code files to my project to hold the interface and class for my service, and copied/pasted the Interface and Class code from my original WCF service library project, and made the alterations that I needed to make.
The new project compiles fine, however...
1) If I go into the project properties, the "WCF Options" section does not appear, as it does in my original WCF service library.
2) With the original WCF service libary project, I could right-click on it and select Debug->Start New Instance, and it would automatically start it using WCF Service Host. With the new project, if I try that, it says, "A project with an Output Type of Class Library cannot be started directly".
How do I get Visual Studio (I'm using 2012 Professional) to see this project as a true WCF class library, short of starting over and using the "WCF Service Library" project template?
You are missing the ProjectExtensions section from your project file
This is from one of my VS2010 projects.
<ProjectExtensions>
<VisualStudio>
<FlavorProperties GUID="{3D9AD99F-2412-4246-B90B-4EAA41C64699}">
<WcfProjectProperties>
<AutoStart>True</AutoStart>
</WcfProjectProperties>
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
If I remove it, the WCF Options properties section goes away.
EDIT:
I did a comparison of an Empty project to a WCF Service Library, and found that you also need to add this to your project to get the WCF Options to appear in the project properties
<ProjectTypeGuids>{3D9AD99F-2412-4246-B90B-4EAA41C64699};{F184B08F-C81C-45F6-A57F-5ABD9991F28F}</ProjectTypeGuids>
There is another entry in WCF (and not in the Empty project)
<StartArguments>/client:"WcfTestClient.exe"</StartArguments>
which I'm assuming is needed when you do the
Debug->Start New Instance
There is other stuff that you will need however, such as a reference to System.ServiceModel.
To be honest, I think you'd be better off using the proper WCF Service Library template and cutting out stuff you don't want, rather than trying to figure out what you do need.

WCF service operations not updated

I´m creating a new WCF service. I initially had only three operations. But after some time I decided to add two more. This operations doesn't appear in the Microsoft test client, neither in the list of operations when I try to add a service reference from my WPF client. Also I tried to comment one of the initial operations. It still apears in the Microsoft test client and can be invoked. I Tried also delete the dlls generated by the service and regenerate again. No luck. There are some kind of "cache" where Visual Studio stores the WCF services libraries that I can delete?
UPDATE: I'm working with the service running in the ASP.NET devolopment server.
You need to understand the order in which things happen.
You change your code, adding methods with [OperationContract] on them, or removing them, or changing their parameters or return values.
You then must build your service, producing a .DLL that contains the changes.
You must then deploy the changed DLL to the server it's going to run on
You must then restart the service (this may happen automatically depending on the server. For instance, IIS will recycle the service when it sees that the DLL changed)
You must then update your client, either the WCF Test Client, or "Add Service Reference", or the equivalent.
This last will have the effect of sending a request to the service for the new metadata or WSDL. Only then can the client see the changes you made to the definition of the service.
I don't know why, but I created a new project and copied the definitions of the operations from the problematic project and the problem is gone. One case more for Microsoft mysteries.
Make sure you are updating the services after adding the new operations.
Also make sure they have the attribute [OperationContract].
One thing we have discovered is that when you deploy the dlls that they must be in the bin, and cannot reside in the debug or release folder.
For me worked: just rebuild the wcf project
Did you close the client connection in client side
as showing your service
class Test
{
static void Main()
{
LocationClient client = new LocationClient();
// Use the 'client' variable to call operations on the service.
// Always close the client.
client.Close();
}
}
SOLUTION HERE :
Make sure your dataContract does NOT contain any enum
(You can use integer instead)
Be sure to reference a project in the solution and not a dll on your disk
Remove your "bin" and "obj" folders
Recompile
In IIS recycle the application pool
In IIS restart your service
In IIS "Browse" your service
=> You got it

Adding information to my svcfile without a web-project?

I'm following this guide here:
http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/09/16/integrating-structuremap-and-nhibernate-with-wcf.aspx
And has now come to the end of it where i need to add :
<%# ServiceHost Language="C#" Debug="true" Service="Wcf.ComboService" Factory="Wcf.DIServiceHostFactory" %>
to my svcfile,
the problem is that i dont have an svcfile and according to this post you must add a reference from a Web-application to get it:
Adding .svc file missing
Is there really no way to do this? or maybe make this code work without the svc-file?
An .svc file is just a text file with a different extension, so you can always add it as a text file, no matter what sort of Visual Studio solution you use.
It's not compiled, but rather sits alongside the WCF binaries, instructing IIS on how to create service instances. If configured correctly, IIS interprets the .svc file and spins up the service from the binaries.
However, if you don't want to host your service in IIS, you don't need an .svc file. You can just use the ServiceHostFactory to manually spin up the service:
var factory = new Wcf.DIServiceHostFactory();
var host = factory.CreateServiceHost(typeof(Wcf.ComboService), baseAddresses);
host.Open();
This is called self-hosting.

Connection string in app.config in a class library

I am creating solution and inside I have three projects:
A WCF Service Library Project
A DataAccess Project (Class Library)
A Web site for hosting WCF service
The implementation of the service is on the project # 1, but in order to access the DataBase I use a second project that implements the data access using a class library project.
That problem is in order to get data access I need to configure a connection string, but that connection string must be configurable in a production environment, I meant in production I am going to deploy the site, which is a very simple project that contains only a reference WCF Service Library Project then a guy from database department will configure the connection string.
In development I have an app.config on the data access project but when I do the release that app.config is embedded on the dll.
Any ideas how can we achieve our purpose
The connection string must be in the application configuration file of the executing assembly. This means that you can provided the configuration file for your assembly along with the assembly itself but anyone who wants to use your assembly must update their configuration file to include the values that your assembly relies on.
The connection string in your app.config (data layer) is not embedded in the dll.
If you look in the app.config file in your data layer project, you will probably have a connectionStrings section. you need to put the connectionStrings in the web.config of your WCF service website.
This can be configured in your production environment.
I had a mistake, I was using a different name on the web.config of the WCF site, I just copy the the exact part of the app.config to the web.config and its working now.
Thanks for your help