How I access app settings key inside web.config - c#

I have many services with the same main path, so I'm looking to add the main path to app settings keys and access it on baseAddress.
just as below code, what I could use instead of {baseAddress}?
<appSettings>
<add key="baseAddresses"value="https://localhost:4434/X/"/>
</appSettings>
<service name="X.Services.Proxy.MOF" behaviorConfiguration="restServiceBehave">
<host>
<baseAddresses>
<add baseAddress="{baseAddresses}/MOF.svc" />
</baseAddresses>
</host>
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="rest" bindingConfiguration="NormalRequestBinding" contract="X.Services.Proxy.MOF" name="MOF" />
</service>

You can do it:
ConfigurationManager.AppSettings["{your_key}"]

Related

Custom Configuration Class with KeyValue pairs inside

I want to implement the following configuration section:
<Services>
<Service name="Service-1" provider="Provider-1" type="Type-1">
<add key="Region" value="region-name-1"/>
<add key="AccessKey" value="1234567890"/>
<add key="SecretKey" value="asdfghhkl"/>
</Service>
<Service name="Service-2" provider="Provider-1" type="Type-2">
<add key="Region" value="region-name-2"/>
<add key="EndpointName" value="tyudfv.endpoint.com"/>
<add key="AccessKey" value="1234567890"/>
<add key="SecretKey" value="asdfghhkl"/>
</Service>
</Services>
And I'm struggling with two issues:
1. Retrieve list of nodes without wrapping with some parent ConfigurationSection
2. Retrieve KeyValue collection of node
Is there some obvious way here? Or it's just such complicated as I think?

One WCF service, multiple endpoints, different context per endpoint

I'm writing a multi-tenant WCF application. I've got one "general" service class that is responsible for executing some business logic. I'd like to configure WCF with Autofac in this way that each endpoint will receive different configuration params using custom configuration section e.g.:
<system.serviceModel>
<services>
<service name="MyApiService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:80000" />
</baseAddresses>
</host>
<endpoint name="Tenant1" address="tenant1" binding="basicHttpBinding" contract="IMyApiService" />
<endpoint name="Tenant2" address="tenant2" binding="basicHttpBinding" contract="IMyApiService" />
</service>
</services>
</system.serviceModel>
<apiConfigurationSection>
<service name="Tenant1" wcfServiceName="MyApiService" param1="value1" param2="value2" />
<service name="Tenant2" wcfServiceName="MyApiService" param1="value3" param2="value4" />
</apiConfigurationSection>
I'd like parameters param1 and param to be injected as a contex when matching service (by URL) is called.
Could you suggest me any solution how to do this?

Only an absolute URI can be used as a base address

Please help getting exception at using (ServiceHost host = new ServiceHost(typeof(HelloService.HelloService))) in the below code
Exception : Only an absolute URI can be used as a base address
WCF Host Application
class Program
{
static void Main()
{
using (ServiceHost host = new ServiceHost(typeof(HelloService.HelloService)))
{
host.Open();
Console.WriteLine("Service Started");
Console.ReadLine();
}
}
}
Contract Implementation
public class HelloService : IHelloService
{
public string GetMessage(string Name)
{
return "Hello" + Name;
}
}
Contract
[ServiceContract]
public interface IHelloService
{
[OperationContract]
string GetMessage(string Name);
}
App.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="HelloService.HelloService" behaviorConfiguration="mexBehaviour">
<endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService">
</endpoint>
<endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/HelloService"/>
<add baseAddress="net.tcp//localhost:8090/HelloService"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I believe you are missing a colon (:):
<add baseAddress="net.tcp//localhost:8090/HelloService"/>
should be
<add baseAddress="net.tcp://localhost:8090/HelloService"/>
<endpoint address="HelloService"...
Should be
<endpoint address="/HelloService"...
See https://msdn.microsoft.com/en-us/library/ms733749(v=vs.110).aspx
In short this is Error related to base address.so, please check whether you have entered base address properly or not.
Error:
<add baseAddress=" "http://localhost:8732/Design_Time_Addresses/WcfServiceLibraryShop/IStudent/">" />
Solved:
<add baseAddress=" http://localhost:8732/Design_Time_Addresses/WcfServiceLibraryShop/IStudent/" />

WCF RestFul service increasing response time with number of concurrent users

I am fairly new to WCF. I have created a WCF RestFul service which internally has a call to google geocoding api service and a database call. As the number of concurrent users are increased, there is a drastic increase in the average response time. I am using SoapUI for performance testing. Below is a table for the variation in average response time. Could someone please advise on what might be going wrong and what all things I need to consider to get Response time < 5 sec for upto 100 threads. Thanks in advance.
(No. of Threads/Sec) (Response Time in msec Run 1) (Response Time in msec Run 2) (Response Time in msec Run 3)
1 user 1490 517 585
2 users 2264.5 785.5 853.5
3 users 1341.33 1124 1093.33
4 users 2479.75 1400.25 1317
5 users 2348.6 1432.4 1673.4
8 users 4818.25 2130.25 4726.3
10 users 7510.8 8518.2 7655.4
15 users 11869.26 12933.93 10592.2
20 users 13510.4 6319.75 14553.1
25 users 17695.6 17695.6 17695.6
35 users 20905.17 19823.2 21936.5
50 users 26235.25 26146.32 31645.87
100 users 38424.488 37417.6 39465
Edit:
Web.Config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="user" value="Mahesh" />
<add key="GeoCodingErrorDesc" value="GeoCodingErrorDesc." />
<add key="GeoCodingErrorCode" value="1111" />
<add key="CacheEmptyErrorDesc" value="CacheEmptyErrorDesc." />
<add key="CacheEmptyErrorCode" value="2222" />
<add key="DATAValidationErrorDesc" value="Schema valiation failed for the input recieved." />
<add key="DATAValidationErrorCode" value="5555" />
<add key="GenericErrorDesc" value="GenericErrorDesc." />
<add key="GenericErrorCode" value="3333" />
<add key="GeoCodingAPIKey" value="*****" />
<add key="Geocountry" value="|country:IN" />
<add key="GeoCodingAPIUrl" value="http://maps.googleapis.com/maps/api/geocode/xml?address=" />
<add key="Connectionstring" value="Data Source = MachineName; Integrated Security=True;" />
<add key="Query" value="SELECT * FROM Table where Code IN " />
</appSettings>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ITwoWayAsyncVoid" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost/ESB.ExceptionHandlingServices.WCF/ExceptionHandling.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_ITwoWayAsyncVoid"
contract="ExceptionHandlingService.ExceptionHandling" name="WSHttpBinding_ITwoWayAsyncVoid">
</endpoint>
</client>
<services>
<service name="SearchRequest">
<endpoint address="" binding="webHttpBinding" behaviorConfiguration="web" contract="ISearchRequest">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/Search/SearchRequest.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
My interface looks as below,
[ServiceContract]
public interface ISearchRequest
{
[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped, UriTemplate = "")]
Stream GetList(Stream value);
}

Configure Service Model for WCF

I have a very basic webservice using WCF (C#, .NET 4.0), to return an hello message.
The deployment under IIS 7 and running it is ok, but when I do svcutil.exe http://localhost:4569/Service.svc?wsdl through the CMD to test the webservice I get:
the remote server returned an error: 415 cannot proccess the message
because the content type 'aplication/soap+xml charset=utf8' was not
the expected type 'text/xml charset=utf8'
When trying to add the service reference (to create a client) I get
An existing connection was forcibly closed by the remote host Metadata
contains a reference that cannot be resolved:
'http://localhost:4569/Service.svc'. Content Type
application/soap+xml; charset=utf-8 was not supported by service
http://localhost:4569/Service.svc. The client and service bindings
may be mismatched. The remote server returned an error: (415) Cannot
process the message because the content type 'application/soap+xml;
charset=utf-8' was not the expected type 'text/xml; charset=utf-8'..
I'm pretty sure that the problem is under my Web.config file:
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service name="Service">
<endpoint name="soap"
address="http://localhost:4569/Service.svc"
binding="basicHttpBinding"
contract="IService" />
<endpoint name="mex" address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</services>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
Anyway, here's my code:
IService.cs:
[ServiceContract]
public interface IService
{
[OperationContract]
string getMessage();
}
My service.cs has the method
public class Service : IService
{
public string getMessage()
{
return "Ola servico";
}
}
I really don't know what is happening, did some tests after some research but no success.
Service.svc
<%# ServiceHost Language="C#" Debug="true" Service="Service" CodeBehind="~/App_Code/Service.cs" %>
You have no service and endpoint defined in your config. Try adding
<services>
<service name="Service"> <!-- name should match the name in your .svc file (if you open it with a text editor) -->
<endpoint name="soap" address="" binding="basicHttpBinding" contract="IService" />
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>

Categories

Resources