how to use AppSettings of app.config in itself - c#

I have a wcf config in client side. I need use a key in AppSettings by ServerIP Name for other sections in App.Config instead of localhost, Because count of my endpoints is many and my server ip is variable. How can I it?
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="ServerIP" value="localhost"/>
</appSettings>
<system.serviceModel>
<bindings>
...
</bindings>
<client>
<endpoint address="net.tcp://localhost:9000/WcfServices/Person/PersonService"
binding="netTcpBinding" bindingConfiguration="RCISPNetTcpBindingWpf"
contract="Common.ServiceContract.IPersonService" name="BasicHttpBinding_IPersonService">
<identity>
<dns value="localhost" /> <!--How use ServerIP in appSettings instead of localhost-->
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>

Normally you can not do that, but you might be able to use web.config transformations if you are using VisualStudio 2010. I think you have to use web deploy, or run an MSBuild task yourself to run the transforms.

you could not use appsettings in app.config itself.
you have two possibilities:
-use appsettings by code like making your our custom section
http://haacked.com/archive/2007/03/12/custom-configuration-sections-in-3-easy-steps.aspx
-use a nant build or msbuild with a placeholder
How to use MSbuild property in TransformXml task?

Related

App.Config in C# Solution

I have a question concerning the app.config files in a c# solution. My solution contains 6 projects, among them are 4 Class Library projects, a WCF Application project and a Windows Forms project. The WCF project and Windows Forms project both contain an app.config file, however in the WCF project it is named "app.config" and the Windows Forms version is "App.config". I placed a reference for a connection string in the "App.config" version for one of the Class Library projects and I was wondering how the solution knew which config file to use. I have included both files below. If any could help me understand this that would be great. Thanks!!!
App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<connectionStrings>
<add name="medFactors" connectionString="Data Source=JDH8865-1\SQLEXPRESS;Initial Catalog=MedicalFactors;Integrated Security=True"/>
</connectionStrings>
<appSettings>
<!--<add key="RepositoryType" value="FactorsRepositoryService.WCFServiceRepository,FactorsRepositoryService, Version=1.0.0.0, Culture=neutral"/>
<add key="RepositoryType" value="FactorsRepositoryCSV.CSVRepository,FactorsRepositoryCSV, Version=1.0.0.0, Culture=neutral"/>-->
<add key="RepositoryType" value="FactorsRepositorySQL.SQLRepository,FactorsRepositorySQL, Version=1.0.0.0, Culture=neutral"/>
<add key="CSVFileName" value="tbl_Zip-Factors.csv"/>
</appSettings>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IPersonService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53959/MedFactorsService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IPersonService" contract="WCFMedFactorService.IMedFactorsService"
name="BasicHttpBinding_IPersonService" />
</client>
</system.serviceModel>
</configuration>
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMedFactorsService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:53959/MedFactorsService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMedFactorsService"
contract="WCFMedFactorService.IMedFactorsService" name="BasicHttpBinding_IMedFactorsService" />
</client>
</system.serviceModel>
</configuration>
OK, let me complete the answer.
The App.Config file of a project that is the starting project of a solution, is used as the config file for the solution.

Facing issue with WCF: Could not find default endpoint

I created a WCF service in separate project name is AaWs then added this project in a website project as a reference . website project name is AaNuWs
in website project web.config file is
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="True"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="mexBehavior" name="AaWs.RxtraIntroService">
<endpoint address ="AaWs" binding="basicHttpBinding" contract="AaWs.IRxIntro">
</endpoint>
<host>
<baseAddresses>
<add baseAddress = "http://localhost:8080" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
then uploaded this website project on my web server.
then give the service reference in my new project from my website with a name space
IntroService
http://mywebsite/IntroService.svc?wsdl
new system create a new app.config file for me
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IRxIntro" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://mywebsite/IntroService.svc/RecoverInfoTechAaWs"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IRxIntro"
contract="IntroService.IRxIntro" name="BasicHttpBinding_IRxIntro" />
</client>
</system.serviceModel>
</configuration>
but now when i trying to access this service i am getting a error.
System.InvalidOperationException: Could not find default endpoint
element that references contract 'IntroService.IRxIntro' 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.
please help me on this
You need to confirm that the app.config is in the root folder with your executable. Then confirm that the app.config has the proper value for:
<client>
<endpoint ... contract=""
From the information you provided, the address information does not look correct. Your service has an overall address of "http://localhost:8080/AaWs" but your service reference (wsdl url) shows "http://mywebsite/IntroService.svc?wsdl". You surely want to make sure that you are pointing to the correct service when generating the reference.
Remove name="BasicHttpBinding_IRxIntro" attribute from endpoint configuration of app.config.

WCF Service - service in separate class library

This is my project diagram:
MyWCFService
(...)
web.config
Friends.Implementation
Friends.svc
Friends.svc.cs
Friends.Contract
IFriends.cs
My endpoint of service:
<service behaviorConfiguration="ServiceBehaviour" name="Friends.Implementation.Friends">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding" contract="Friends.Contract.IFriends" />
</service>
My question is: How can I call this service? When I had that service in "Service" folder in MyWCFService project, it was easy - just localhost:XX/Services/Friends.svc.
And now, how can I run it, if everything is in separate class libraries?
Regards
You can create a Services folder in your website root and paste the .svc file there, or you can use service activation:
<configuration>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add service="Friends.Implementation.Friends"
relativeAddress="Services/Friends.svc"/>
</serviceActivations>
</serviceHostingEnvironment>
</system.serviceModel>
</configuration>

C# WCF cant see app.config

Ok so here is my scenario.
I have made a WCF Library project. Configured my app.config file. I then created a new console application. Added my WCF Library project to the console app. Then I copied the config file over to my server console app. However, when I run the console application it throws an exception and essentially states that it cant see the app.config file. However, the app.config is clearly inside of the server console application. I can add my endpoints programmatically and make the service work. However, that is not my intention. Is there some sort of trick in order to get the ServiceHost to use the app.config, or more importantly the project to see the app.config?
My app.config
<configuration>
<configSections>
</configSections>
<system.web>
<compilation debug="true" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="serviceBehavior" name="Services">
<endpoint address="CompanyService" binding="netTcpBinding" bindingConfiguration=""
name="NetTcpBinding" contract="Services.ICompany" />
<endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8732/Services/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="serviceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
My Host Service code:
using (ServiceHost host = new ServiceHost(typeof(Company))) {
host.Open();
Console.WriteLine("Press <ENTER> to terminate the host service");
Console.ReadLine();
}
It then throws this exception:
Unhandled Exception: System.InvalidOperationException: Service 'Services.Company' has zero application (non-infrastructure) endpoints. This might
be because no configuration file was found for your application, or because no
service element matching the service name could be found in the configuration fi
le, or because no endpoints were defined in the service element.
What are you naming the app.config file when you copy it over to the console app? There are specific rules that will need to be followed.
If the console app has an executable name of, e.g. "consoleapp.exe", then the app.config will have to have the name "consoleapp.exe.config"
Is the problem that you have called the file app.config, and not yourprogramname.exe.config?
#Jaochim The error says it's looking for a service named Services.Company while your config seems to have it attributed with name="Services" Try changing that.

C# WebService endpoint problem

I'm creating a webservice client. And I have the problem - in MainWindow.xaml there is a message: 'Could not find endpoint element with name 'LightsWSSoap' and contract 'LightsWS.LightsWSSoap' 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 name could be found in the client element.'
In this file I have something like:
xmlns:vm="clr-namespace:LightsClient2.ViewModels"
<Window.DataContext>
<vm:MainWindowViewModel />
</Window.DataContext>
And in the MainWindowViewModel the constructor of webservice is used:
LightsWSSoap lService = new LightsWSSoapClient("LightsWSSoap");
Where 'LightsWS' is the name of the Service.
There is an app.config file and there are enpoints definitions in it:
<client>
<endpoint address="http://xxx/Lights/LightsWS.asmx"
binding="basicHttpBinding" bindingConfiguration="LightsWSSoap"
contract="LightsWS.LightsWSSoap" name="LightsWSSoap" />
<endpoint address="http://xxx/Lights/LightsWS.asmx"
binding="customBinding" bindingConfiguration="LightsWSSoap12"
contract="LightsWS.LightsWSSoap" name="LightsWSSoap12" />
</client>
What is wrong? Any ideas?
You will need to configure your client endpoint in the App.Config file in your WPF application.
If there is not already an App.Config file you can create one by adding a new item to your project and selecting "Application Configuration File".
The contents of App.Config should look something like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<client>
<endpoint binding="wsHttpBinding" contract="LightsWS.LightsWSSoap" name="LightsWSSoap" />
</client>
</system.serviceModel>
</configuration>
There is a lot more detailed information on MSDN - http://msdn.microsoft.com/en-us/library/ms731745%28VS.90%29.aspx.
You should add app.config file with definition of your endpoint. Usually it is create by Visual Studio. But if you created your ServiceReference in another assembly (for example, is separate dll), then just copy app.config from that assembly to your WpfApplication.
Try the ideas in this thread, especially the namespaces.

Categories

Resources