Sharing wcf configurations between applications - how to get/set values programmatically? - c#

Here's the situation: I want to be able to run my (.net 3.5) WCF service in two modes:
as a regular .exe file, and
as a windows service.
not much of a problem.
I create 2 projects: one console application and one windows service.
Now, I don't want to duplicate the WCF configuration twice (for each project), so I put it in separate .xml files (that can be shared by both apps).
Now each of the app.config files looks like this:
<system.serviceModel>
<bindings configSource="Config\ServiceModel.Bindings.xml" />
<services configSource="Config\ServiceModel.Services.xml" />
<behaviors configSource="Config\ServiceModel.Behaviors.xml" />
</system.serviceModel>
So far, so good - the service works fine both as console and as service.
The problem starts when I try to manipulate the values in the services section:
string exePath = string.Format("{0}MyService.exe", targetDir);
var config = ConfigurationManager.OpenExeConfiguration(exePath);
var serviceModelSectionGroup = config.GetSectionGroup("system.serviceModel");
var servicesSection = (ServicesSection) serviceModelSectionGroup.Sections["services"];
var services = servicesSection.Services;
But services is an empty collection!
I'm guessing I'm missing something here, but what? Should I also call OpenExeConfiguration for each of my .xml files?

Related

How can I use a web service from local WSDL file?

I have been given a WSDL file from a provider and I have added it as a Service Reference to my C# project in Visual Studio 2013.
I can see the relevant classes I need but when I call the functions on them nothing is transmitting from my program. I have configured Wireshark to listen but no data is coming from my program when I run it.
Where in Visual Studio can I see the IP address/URL that the web service is trying to connect to? At some point I assume it establishes a HTTP connection, where can I see this code to check the URL/IP address?
The WSDL file does not contain the address of the service endpoint. You probably created a Service Reference or a Web Reference, which has created a client class for you. If you instantiate this client (lets call it ExampleClient) with the default constructor:
var serviceCLient = new ExampleClient();
Then the URI will be the path of the WSDL file you imported. This mostly works fine if you import a generated WSDL file file the actual service URI, but in your case, you need to tell it where the service is running. You can either pass the service URI in the constructor:
var serviceCLient = new ExampleClient("http://example.com/service/endpoint");
Or edit your app.config or web.config (depending on project type). It will have something like this:
<system.serviceModel>
<client>
<endpoint address="C:\path\to\your.wsdl" etc etc etc... />
</client>
</system.serviceModel>
And you should change the address attribute there.
For old style web references, you can right click on the reference in Visual Studio (under your project in the Web References folder, and select "properties". The properties screen contains a "Web Reference Url" which you can edit to point to the actual service URI.

Calling WCF service from class library: App.Config vs Web.Config?

From an existing WebApplication I use to make calls to WCF services. Proxies for these services was created using Add Service Reference menu. Thus generating >> Web.config in this project.
I have added another class library project to the solution. This project also adds reference to the service. Thus generating >> App.Config file in this project.
I understand, in an N-Tier application, we should have common gateway to the service. Just out of curiosity I would like to know -
For WCF calls originated in WebApp, propagated to class library which config file (App/Web) would be referred
for locating client endpoint configurations ?
The web.config file will be used for locating the service.
The reason is because in this case, the app domain belongs to the web app, not the class library, and the default config file for this app domain is the web.config.
#musefan is correct. It is the web.config that is used.
If you want to, you can split some confuguration sections into seperate files and reference them from the main web.config. You might want to do this so you can maintain the WCF client and server config in a single place to ensure they are consistent.
foe example, if you want to seperate out the <client> section, you would do this:
<client configSource="client.xml" />
Where client.xml is a file containing the relevant client config information.
This blog post tells you how to do it in a bit more detail.
http://blog.andreloker.de/post/2008/06/keep-your-config-clean-with-external-config-files.aspx

How to programmatically add domain service end point to web.config

I am in need of help with manipulating the web.config of a site programmatically via C#. The site in question hosts a Silverlight 5 application which communicates with the server runtime via WCF RIA services.
The code that I am writing is part of a bootloader for an automated build-deploy-test scenario aimed at testing the WCF Ria service stack. The issue at question is that in order to test the services properly the unit test code needs to be able to communicate with the Ria Services via a new soap endpoint.
To effectively make this work the site needs a copy of the Microsoft.ServiceModel.DomainServices.Hosting dll in it's bin folder, and a new soap endpoint which would make the domain services config section look like the following:
<system.serviceModel>
<domainServices>
<endpoints>
<add name="OData" .../>
***<add name="Soap" type="Microsoft.ServiceModel.DomainServices.Hosting.SoapXmlEndpointFactory, Microsoft.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>***
</endpoints>
</domainServices>
</system.serviceModel>
While I do believe I could modify the web.config via the C# xml api, I was wondering if there is another way to do so via the ConfigurationManager?
I would do config manipulation as part of the build script.
I am currently using YDeliver as the build/deploy framework in my project and since it runs on top of Powershell, I manipulate XML using the xml api in posh.

applicationSettings for a web application

I know similar flavors of this question has been asked before, but I am asking something a little different. I know how to use applicationSettings and such, but what I am trying to do is to make it into a config. i.e., I have a web application which as a reference to a class library. This class library has a reference to a web service. In doing that, it created a .settings file. When I am using this class library in the web application, everything works fine locally. However, in staging and production, I would like to have different URLs for the web service - the only way to do that seems to take the applicationSettings section, and put it in the web.config of the web application project. I'm trying to avoid muddying up the web.config, so is there a way to have this applicationSettings section in another files referenced by the web.config?
<appsSettings configSource="appSettings.config" />
This moves the whole app settings section to a separate file.

Web service takes long time to run the first time

I am new to writing and deploying web services and I have a web service that when I run it from my C# applications, it will take 4-8 seconds to respond the first call. Subsequent calls are in the half second range. If I call the web service from internet explorer, it returns immediately.
My Web service is a Soap web service running on windows web server 2008 server. I have been looking for a solution for several days and nothing I have tried has helped. The 2 things have tried that don't appear to work is precompile the XmlSerializers (Generate serialization assembly is on) and precompile the web service using aspnet_compiler. The output of aspnet_compiler I copy to the server and paste into the appropriate folder.
My application is calling the web services by using the auto generated code web reference code created when you use the Add Web Reference wizard.
Is there something else I can try?
A common culprit is checking of Publisher Evidence (basically, assembly signatures) in the absense of open connectivity to the internet.
Try adding the following line to your ASPNET.CONFIG or APP.CONFIG file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<runtime>
<generatePublisherEvidence enabled="false"/>
</runtime>
</configuration>
See for background and details online articles, e.g.
http://blogs.msdn.com/b/pfedev/archive/2008/11/26/best-practice-generatepublisherevidence-in-aspnet-config.aspx

Categories

Resources