Programmatically change web service information in app.config


A

Andrew Jocelyn

Hi

How do I programmatically change (read/write) the values in this app.config
file at runtime? Specifically I want to change the client endpoint address
but it would be nice to change other values too. I thought this is what
ServiceHost is for but is seems way more complicated to use for what I'm
trying to do.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DataServiceSoap" closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="01:00:00" sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="67108864" maxBufferPoolSize="524288"
maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://webservice.com/Service.asmx"
binding="basicHttpBinding" bindingConfiguration="DataServiceSoap"
contract="MyService.DataServiceSoap" name="DataServiceSoap" />
</client>
</system.serviceModel>

Many thanks
Andrew
 
Ad

Advertisements

A

Alvin Bruney [ASP.NET MVP]

It's a file, you can read and write it as normal. However, you will restart
the application. Now, step back a bit. Endpoints are established ahead of
time and configured accordingly. Your requirement is about the same as
rewriting a browser url so that clients can hit this new url. Why not
publish the url ahead of time? Why the strange requirement?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
 
J

Jeff Dillon

Alvin Bruney said:
It's a file, you can read and write it as normal. However, you will
restart the application. Now, step back a bit. Endpoints are established
ahead of time and configured accordingly. Your requirement is about the
same as rewriting a browser url so that clients can hit this new url. Why
not publish the url ahead of time? Why the strange requirement?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------


Andrew Jocelyn said:
Hi

How do I programmatically change (read/write) the values in this
app.config file at runtime? Specifically I want to change the client
endpoint address but it would be nice to change other values too. I
thought this is what ServiceHost is for but is seems way more complicated
to use for what I'm trying to do.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DataServiceSoap" closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="01:00:00" sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="67108864" maxBufferPoolSize="524288"
maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://webservice.com/Service.asmx"
binding="basicHttpBinding"
bindingConfiguration="DataServiceSoap"
contract="MyService.DataServiceSoap" name="DataServiceSoap" />
</client>
</system.serviceModel>

Many thanks
Andrew

No, that is not a good solution. How do you programmatically change the
endpoints? We have the same requirement. I suspect we can just define two
endpoints (binding names) in the config as you suggest however, then choose
which one to use at runtime. We have an application that needs to easily
switch between sandbox and production environment web services.

Jeff
 
A

Andrew Jocelyn

OK, this is the situation.

The windows form client is a ClickOnce app and will be downloaded from a
central location. It needs some default config but the webservice it will
need to connect to will be customer specific, i.e. each client will need to
connect to their own URL.

Perhaps this is poor design but it's more secure than allowing all clients
to connect to a single service. I know I can deserialize/serialize the
app.config but there must be a preferred way to modify these particular
settings. I don't want the user to modify the file directly.

Also it seems like there may be occasions when things like maxBufferSize
need to be tweaked so an interface of seems like it should be a common
requirement to me. What do other people do?

Thanks
Andrew



Alvin Bruney said:
It's a file, you can read and write it as normal. However, you will
restart the application. Now, step back a bit. Endpoints are established
ahead of time and configured accordingly. Your requirement is about the
same as rewriting a browser url so that clients can hit this new url. Why
not publish the url ahead of time? Why the strange requirement?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------


Andrew Jocelyn said:
Hi

How do I programmatically change (read/write) the values in this
app.config file at runtime? Specifically I want to change the client
endpoint address but it would be nice to change other values too. I
thought this is what ServiceHost is for but is seems way more complicated
to use for what I'm trying to do.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DataServiceSoap" closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="01:00:00" sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="67108864" maxBufferPoolSize="524288"
maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://webservice.com/Service.asmx"
binding="basicHttpBinding"
bindingConfiguration="DataServiceSoap"
contract="MyService.DataServiceSoap" name="DataServiceSoap" />
</client>
</system.serviceModel>

Many thanks
Andrew
 
J

Jeff Dillon

Andrew Jocelyn said:
OK, this is the situation.

The windows form client is a ClickOnce app and will be downloaded from a
central location. It needs some default config but the webservice it will
need to connect to will be customer specific, i.e. each client will need
to connect to their own URL.

Perhaps this is poor design but it's more secure than allowing all clients
to connect to a single service. I know I can deserialize/serialize the
app.config but there must be a preferred way to modify these particular
settings. I don't want the user to modify the file directly.

Also it seems like there may be occasions when things like maxBufferSize
need to be tweaked so an interface of seems like it should be a common
requirement to me. What do other people do?

Thanks
Andrew



Alvin Bruney said:
It's a file, you can read and write it as normal. However, you will
restart the application. Now, step back a bit. Endpoints are established
ahead of time and configured accordingly. Your requirement is about the
same as rewriting a browser url so that clients can hit this new url. Why
not publish the url ahead of time? Why the strange requirement?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------


Andrew Jocelyn said:
Hi

How do I programmatically change (read/write) the values in this
app.config file at runtime? Specifically I want to change the client
endpoint address but it would be nice to change other values too. I
thought this is what ServiceHost is for but is seems way more
complicated to use for what I'm trying to do.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DataServiceSoap" closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="01:00:00" sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="67108864" maxBufferPoolSize="524288"
maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://webservice.com/Service.asmx"
binding="basicHttpBinding"
bindingConfiguration="DataServiceSoap"
contract="MyService.DataServiceSoap" name="DataServiceSoap" />
</client>
</system.serviceModel>

Many thanks
Andrew

Keep in mind that the web service proxy/client constructor is overloaded and
can take the endpoint address as a parameter
 
A

Alvin Bruney [ASP.NET MVP]

OK, this is the situation.
Yes I suspected that was half the story but I wanted to be polite about it.

The recommended solution is to define multiple endpoints. Bear in mind, WCF
is designed to be coded once and configured appropriately in production.
Things like adding endpoints should not be addressed at the code level, they
should rightly be handled by support professional 'in production' with no
touch to the code.

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------


Andrew Jocelyn said:
OK, this is the situation.

The windows form client is a ClickOnce app and will be downloaded from a
central location. It needs some default config but the webservice it will
need to connect to will be customer specific, i.e. each client will need
to connect to their own URL.

Perhaps this is poor design but it's more secure than allowing all clients
to connect to a single service. I know I can deserialize/serialize the
app.config but there must be a preferred way to modify these particular
settings. I don't want the user to modify the file directly.

Also it seems like there may be occasions when things like maxBufferSize
need to be tweaked so an interface of seems like it should be a common
requirement to me. What do other people do?

Thanks
Andrew



Alvin Bruney said:
It's a file, you can read and write it as normal. However, you will
restart the application. Now, step back a bit. Endpoints are established
ahead of time and configured accordingly. Your requirement is about the
same as rewriting a browser url so that clients can hit this new url. Why
not publish the url ahead of time? Why the strange requirement?

--

Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
Download OWC Black Book, 2nd Edition
Exclusively on www.lulu.com/owc $15.00
Need a free copy of VSTS 2008 w/ MSDN Premium?
http://msmvps.com/blogs/alvin/Default.aspx
-------------------------------------------------------


Andrew Jocelyn said:
Hi

How do I programmatically change (read/write) the values in this
app.config file at runtime? Specifically I want to change the client
endpoint address but it would be nice to change other values too. I
thought this is what ServiceHost is for but is seems way more
complicated to use for what I'm trying to do.

<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="DataServiceSoap" closeTimeout="00:01:00"
openTimeout="00:01:00"
receiveTimeout="01:00:00" sendTimeout="00:01:00"
allowCookies="false"
bypassProxyOnLocal="false"
hostNameComparisonMode="StrongWildcard"
maxBufferSize="67108864" maxBufferPoolSize="524288"
maxReceivedMessageSize="67108864"
messageEncoding="Text" textEncoding="utf-8"
transferMode="Buffered"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192"
maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None"
proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName"
algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://webservice.com/Service.asmx"
binding="basicHttpBinding"
bindingConfiguration="DataServiceSoap"
contract="MyService.DataServiceSoap" name="DataServiceSoap" />
</client>
</system.serviceModel>

Many thanks
Andrew
 
Ad

Advertisements

A

Andrew Jocelyn

Thanks Jeff
Keep in mind that the web service proxy/client constructor is overloaded
and can take the endpoint address as a parameter

That's the answer I was looking for. For now anyway.

Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top