Dynamically switch WCF Web Service Reference URL path through config file - c#

How do you dynamically switch WCF Web Service Reference URL path through config file ?

Are you just wanting to override the URL that is in the config to a different url. Say you have a test service and a live service. You can just do this.
client.Endpoint.Address = new EndpointAddress(Server.IsLiveServer() ?
#"LiveUrl" : #"TestURl");
Where those url come from wherever you want

Just to expand on the answer from Erin: -
MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress(new Uri("insert new url here"),
client.Endpoint.Address.Identity, client.Endpoint.Address.Headers);
client.Open();
HTH!

There is no dynamic switching. Each time you want to use another URL you must create new instance of service proxy (client) and pass EndpointAddress or enpoint configuration name to the constructor.

I have been trying to do the same thing but most of the accepted answers in various posts just change the address. Currently under .net 4.7, simply changing the address itself does not work. If you have two different servers and wanting it to switch from one to the other, you have to do this:
var client = new MyService.Service1Client();
var newAdrEndpoint = new EndpointAddress(new Uri("second server address"));
client = new MyService.Service1Client(client.Endpoint.Binding, newAdrEndpoint);
Essentially you need to create a new service using the same binding from the first server and passing in the new address. This is the simplest method I have found.

sure you can do this, have a look here: How to config clients for a wcf service?
it is absolutely normal to point to localhost in development and to change the address (url) in production in the web.config

you canĀ“t chance endpoint url after any calling.
E.G.
in that case, you will get answer from NEWURL:
MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress("NEWURL");
client.Hello(); //return is hello response from NEWURL
but if you will call any method before changing url, the url will be used from app.config, like next example:
MyClient client = new MyService.MyClient();
client.Endpoint.Address = new EndpointAddress("NEWURL");
client.Hello(); //return is hello response from BASEURL

Related

IP instead of localhost in URI

I am currently trying to create a valid link to a function of my API using this LOC:
Uri locationHeader = new Uri(Url.Link("GetUserById", new { id = user.Id }));
This returns the following:
http://localhost:53800/..../user/821105b1
However, as the link should be accessible from the network, I would need something like the following:
http://192.168.0.12:53800/..../user/821105b1
How can I get this result instead of the one with the localhost??
Thanks in advance!
Calling local libraries will only give you the local IP. You can call an external API like whatismyip or checkip to get the external IP. See here...https://stackoverflow.com/a/7838551

WCF Client Side Configuration for different Endpoint URI

I am in a situation where I need to develop a WCF Client which will have different EndPoint URI but other settings would remain same. I would get the EndPoint URI from the user.
So I wanted to know if I consume the WCF service using ChannelFactory, then do I need to have app.config file which would contain the WCF Client side configuration with only one endpoint and the address attribute would be blank (which I would get as input from the user) Or do I need to go for programmatically consuming the service.
Leave the endpoint blank in config file. In your code add a method like the one below that takes endpointAddress as a parameter which can come from the user. Use this method to create the channelfactory that you will eventually use to create proxy
private ChannelFactory<IService1> GetChannelFactory(string endpointAddress)
{
// create a binding that will be common
BasicHttpBinding myBinding = new BasicHttpBinding();
//get your uri from the user
EndpointAddress myEndpoint = new EndpointAddress(endpointAddress);
ChannelFactory<IService1> myChannelFactory = new ChannelFactory<IService1>(myBinding, myEndpoint);
return myChannelFactory;
}

How to use proxy at LinqToTwitter

I want to use proxy when using LinqToTwitter. I downloaded source code but there is no proxy feature at HttpWebRequest object. I don't understand how is this happening.
Here what i mean
This is inside TwitterExecute.cs.
I want to modify it in a way that i can set different proxy for each thread.
So my question is how to modify to use proxy server when posting a status tweet etc?
The proxy you're looking at supports Silverlight. I recently added a Proxy property to ITwitterAuthorizer:
https://linqtotwitter.codeplex.com/SourceControl/latest#LinqToTwitterAg/OAuth/ITwitterAuthorizer.cs
This is type WebProxy, which you can assign whenever you instantiate an authorizer:
http://msdn.microsoft.com/en-us/library/system.net.webproxy(v=vs.110).aspx
Updated Sample Code:
var auth = new SingleUserAuthorizer
{
Credentials = new InMemoryCredentials
{
ConsumerKey = srtwitterConsumerKey,
ConsumerSecret = srtwitterConsumerSecret,
OAuthToken = srtwitterOAuthToken,
AccessToken = srtwitterAccessToken
}
};
auth.Proxy = new WebProxy("http://proxyserver:80/",true);
var twitterContext = new TwitterContext(auth);
I've also seen where people have used configuration files to specify their proxy since LINQ to Twitter v2.1.x uses HttpWebRequest:
http://msdn.microsoft.com/en-us/library/kd3cf2ex(v=vs.110).aspx
LINQ to Twitter v3.0 supports IWebProxy on IAuthorizer:
https://linqtotwitter.codeplex.com/SourceControl/latest#LinqToTwitterPcl/Security/IAuthorizer.cs
I just found the folder called LinqToTwitterProxy inside the LinQToTwitter Package. It contains workaround to bypass the proxy.
I hope that will helpout to resolve your issue.

Dynamically creating endpoints for Magento in C#

I need to dynamically set the endpoint for my Magento implementation using C# but can't override C#'s default check of the endpoint path and credentials in the web.config.
Does anyone know how to do this?
My service currently looks like this:
using (Mage_Api_Model_Server_V2_HandlerPortTypeClient proxy = new Mage_Api_Model_Server_V2_HandlerPortTypeClient("NameOfEndpoint", ConnectionCurrent.WsdlPath))
{
string sessionKey = proxy.startSession();
string loginSession = proxy.login(ConnectionCurrent.UserName, ConnectionCurrent.Password);
...
At Login, it then says that I have two endpoints configured.
I've looked everywhere but can't find a solution.
Thanks!!
This is using WCF but it is similarly done with the older web services implementation:
EndpointAddress endPoint = new EndpointAddress("http://some.endpoint.addr");
Binding binding = new WSHttpBinding(SecurityMode.None);
var service = new Mage_Api_Model_Server_V2_HandlerPortTypeClient(binding, endpoint);

Setting the service URL at runtime

When I am adding the "Web Reference" we are giving the address to the asmx page to visual studio.
How Can I set this at run time?
I would have upvoted one of the other answers - they're almost correct.
using (YourService service = new YourService())
{
service.Url = "http://some.other.url/";
// Now you're ready to call your service method
service.SomeUsefulMethod();
}
If a using block is not used, and an exception is thrown, then resources like network connections can be leaked.
Just set the Url property of the object before you call any of the service methods:
YourService service = new YourService();
service.Url = "http://some.other.url/";
// Now you're ready to call your service method
service.SomeUsefulMethod();
YourWebService service = new YourWebService();
service.Url = "http://www.example.com/YourWebService.asmx";
service.CallMethod();

Categories