how to get this config value from app.config? - c#

My friend has the following app.config. He wants to get the value of address. how to do it?
<configuration>
<system.serviceModel>
...
<client>
<endpoint address="http://ldo:8080/LLService" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ILLService" contract="LLServiceReference.ILLService"
name="WSHttpBinding_ILLService">
<identity>
<userPrincipalName value="ggldoe#mail.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
...
</configuration>

try this to Get first endpoint
Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal);
ServiceModelSectionGroup serviceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
ClientSection clientSection = serviceModelSectionGroup.Client;
var el = clientSection.Endpoints[0];
return el.Address.ToString();

Take a look at the <system.serviceModel> documentation in MSDN.
You should:
Call the ServiceModelSectionGroup.GetSectionGroup method
Choose an endpoint from the serviceModelSectionGroup.Client.Endpoints collection. Presumably you want to look at a specific contract.
Look at that endpoint's Address property

Related

Programmatically define the Endpoint and Binding

Warping the call of a web service in a Class Library, I had to past the endpoint definition from app.config to other project that was using the Library.
In order to get rid of that I try to translate my app.config setting into explicit in order to pass Binding and Endpoint definition to the ClientBase.
var client = new myServiceClient(customBinding, custonEndpoint);
I'm looking for a way to translate system.serviceModel definition into code.
And the simpliest definition is giving me issue. For example with the following configuration:
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_MyService" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://address:port/Foo/Services/MyService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_MyService"
contract="MyService.MyService" name="NetTcpBinding_MyService">
<identity>
<userPrincipalName value="FooBar#SecondLevel.TopLevel" />
</identity>
</endpoint>
</client>
</system.serviceModel>
I find no property that match those parameter in NetTcpBinding neither in the Security property:
var endPoint = new EndpointAddress(__EndPoint);
var binding = new NetTcpBinding();
binding.Name = "";
Resulting in an error SSPI.

Cannot set service name attribute in web.config for WCF web service

I have a WCF web service I wrote which works fine, I then copied the contents of the web service from another application and created a new WCF file which created a new in web.config but the name attribute says the namespace cannot be found.
Here is an example of the first few lines of my WCF:
namespace Testing.ws.mainGrid
{
[WebService(Namespace = "")]
[ScriptService]
[ToolboxItem(false)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Test : WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
[WebMethod]
public void GetRecords()
{
((NameValueCollection)new WebClient().Headers).Add("accept", "text/xml");
string str1 = "1";
string str2 = "1";
string str3 = "1";
Here is my web.config:
<system.serviceModel>
<services>
<service name="Testing.ws.getStaffTree">
<endpoint address="" behaviorConfiguration="Testing.ws.getStaffTreeAspNetAjaxBehavior"
binding="webHttpBinding" contract="Testing.ws.getStaffTree" />
</service>
<service name="Testing.ws.mainGrid.Test">
<endpoint address="" behaviorConfiguration="Testing.ws.mainGridAspNetAjaxBehavior"
binding="webHttpBinding" contract="Testing.ws.mainGrid" />
</service>
</services>
Basically name="Testing.ws.mainGrid.Test" does not work and it cannot find it.
I'm not sure what i'm doing wrong but i've spent ages on this! This first works fine but its the second one with the issue.
The actual error is:
the 'name' attribute is invalid - The value Testing.ws.mainGrid.Test is invalid according to its datatype 'serviceNameType' - The enumeration constraint failed
If you let intellisense do its work then it displays the first web service but not my new one!
Your second service should be as follows I think. Note the change in contract name:
<service name="Testing.ws.mainGrid.Test">
<endpoint address="" behaviorConfiguration="Testing.ws.mainGridAspNetAjaxBehavior"
binding="webHttpBinding" contract="Testing.ws.mainGrid.Test" />
</service>
Before it was pointing to the namespace, not the class type.
While we had the same issue, our fix was slightly different due to our development environment using windows authentication for some services while specifically disabling it through the web.config.
Needed to comment out/remove the following section of the web.config
<system.webServer>
<!--<security>
<authentication>
<windowsAuthentication enabled="false" />
</authentication>
</security>-->
</system.webServer>
This needs to be un-commented for publication to production is the only thing.

custom endpoint metadata in app.config file

I've got a app.config file for my application which defines a series of web services similar to this:
<client>
<endpoint address="https://xxxxxx.local:12610/Applicationws.svc"
binding="basicHttpBinding" bindingConfiguration="test1"
contract="Applicationws.Applicationws" name="test1" />
<endpoint address="https://yyyyyy.local:12610/Applicationws.svc"
binding="basicHttpBinding" bindingConfiguration="test2"
contract="Applicationws.Applicationws" name="test2" />
<endpoint address="https://zzzzzz.local:12610/Applicationws.svc"
binding="basicHttpBinding" bindingConfiguration="test3"
contract="Applicationws.Applicationws" name="test3" />
</client>
I'd like to be able to add some extra attributes to each endpoint entry so I can easily identify it. These would be items such as Description, Type etc. and then be able to get this back in the ChannelEndpointElement object or similar e.g.
<endpoint address="https://zzzzzz.local:12610/Applicationws.svc"
binding="basicHttpBinding" bindingConfiguration="test3"
ontract="Applicationws.Applicationws" name="test3"
description="some web service" type="myappname" />
I've had a nose around the net and tried reading the docs but I cannot find anything close. Is it really such an unreasonable thing to want to do? My final thought is to append the extra attributes to the name value with a separator of some kind.

How do I configure the buffer size and max message size in the ASP.NET Web API

We used to have these properties in the WCF Web API configuration.
MaxBufferSize = int.MaxValue,
MaxReceivedMessageSize = int.MaxValue,
If you're self-hosting, it's part of the HttpSelfHostConfiguration class:
MSDN Documentation of the HttpSelfHostConfiguration class
It would be used like this:
var config = new HttpSelfHostConfiguration(baseAddress);
config.MaxReceivedMessageSize = int.MaxValue;
config.MaxBufferSize = int.MaxValue;
You may want to look at httpRuntime section in web.config of your ASP.NET application. They are not named exactly the same, but there may be an analog for what you're trying to do. For instance:
<configuration>
<system.web>
<httpRuntime maxRequestLength="16384" requestLengthDiskThreshold="16384"/>
</system.web>
</configuration>
Note: Int32.MaxValue is 2,147,483,647
first i would have done this configuration in the WCF App.config
<endpoint address ="" binding="basicHttpBinding" bindingConfiguration="basicHttp" contract="AddSubService.IService1">
<!--
Upon deployment, the following identity element should be removed or replaced to reflect the
identity under which the deployed service runs. If removed, WCF will infer an appropriate identity
automatically.
-->
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<!-- Metadata Endpoints -->
<!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
<!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<bindings>
<basicHttpBinding>
<binding name="basicHttp" allowCookies="true"
maxReceivedMessageSize="20000000"
maxBufferSize="20000000"
maxBufferPoolSize="20000000">
<readerQuotas maxDepth="32"
maxArrayLength="200000000"
maxStringContentLength="200000000"/>
</binding>
</basicHttpBinding>
</bindings>
Then try updaeting the reference in the ASP.NET side which is calling the WCF in the web.config check that the endpoint has changed to the same.
I solve this problem making a dynamic binding before like this
BasicHttpBinding bind = new BasicHttpBinding();
bind.OpenTimeout = bind.CloseTimeout = bind.SendTimeout = bind.ReceiveTimeout = new TimeSpan(0, 30, 0);
bind.MaxBufferSize = int.MaxValue;
bind.MaxReceivedMessageSize = int.MaxValue;

Setting WCF Endpoint address at runtime?

If I have the following:
WSHttpBinding binding = new WSHttpBinding();
EndpointAddress endpoint = new EndpointAddress(new Uri("http://xxx:pppp/MyService"));
MyServiceClient client = new MyServiceClient(binding, endpoint);
How can I set the endpoint bindingConfiguration? If it helps my app.config is set to:
<endpoint address="http://xxx:pppp/Design_Time_Addresses/WcfServiceLibrary/ManagementService/"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IManagementService"
contract="ServiceReference.IManagementService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
However I'm looking to let the user configure this before running the client.
Thanks
Very simple fix!! Sorry to ask a silly question!
binding = new WSHttpBinding("WSHttpBinding_IManagementService");
To set your binding administratively you need to add a binding section to your app.config file:
<system.serviceModel>
{...}
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_IManagementService" {other parameters ...} />
</wsHttpBinding>
</bindings>
{...}
</system.serviceModel>
And if you do not feel confortable with the manual editing, you can use the WCF Service Configuration Editor which you can find in the Visual Studio menu Tools>WCF Service Configuration Editor.

Categories

Resources