Dynamic Webservice reference from Class Library used in Winforms app (c#) - c#

Ok - pretty basic scenario, been there before, seemed all so simple - but can't recall enough to work out what's different about the setup at this particular existing codebase.
Winforms App calls Dll which calls Web Service. Reference in the Dll to the Web Service is dynamic. How do I get the URI for the Web Service into a Winforms app.config so I can easily change it for test, dev, live etc.
[Oh just to make it interesting, though I can't see it mattering, the proxy for the web service needs to NOT be regenerated as we have customised it...]

Set the URL directly in your code.
YourServiceProxy service = new YourServiceProxy();
service.Url = ConfigurationManager.AppSettings["YourURLKey"];

Can you configure the web service URI dynamically in code? That way you can easily modify the service to point to the desired location.
You can set the Url property of the webservice in code to point to the URI and use Proxy to set the proxy to your custom proxy.

What's wrong with just copying the URL from the app.config of the library into the app.config of the Windows Forms application?
Also, I'll suggest strongly that you do not modify generated code, ever. You can make many customizations of the proxy by using partial classes. See Ways to Customize your ASMX Client Proxy.

Related

WCF Service and ajax call in same project

I hav a C# web application. In that I have added a WCF Service file (.svc) by Right Click project Add New Item >> WCF Service (wcfService.svc). ( Now I have IwcfService.cs and wcfService.cs in my App_Code folder) And also added a function WCFXmlData(string id) inside that.
I am trying to access the function inside wcf service file from an ajax call in my application (in an aspx file).
But I am not able to do that.
Also I tried to browse this .svc file directly. There I got a message like Metadata publishing for this service is currently disabled.
That really depends on your binding, if you use the WebHttpBinding you can simple access the data via a browser. In best case you should modify your contract in such a way that it returns JSON, this is less overhead than XML or even SOAP (which uses also XML).
It your webpage is also implemented in the webservice you have nothing special to care about, but if your service runs under another subdomain you need to implement JSONP or Cross-Origin Resource Sharing (CORS) to manage cross domain calls.

SOAP WebService... How?

I have a sharepoint web service running at specified host + "/_vti_bin/UserProfileWebService.asmx". I would like to instantiate a web service client, so that i could interact with this service. This is basically what I am trying to do: http://msdn.microsoft.com/en-us/library/ms550407.aspx.
How is this done in .NET? And do I have to use the MOSS API to know what functions it supports?
EDIT:
I guess I should add that I want to connect programmatically, not through a web reference, because that seems to be static.
In VS (2008/2010) you can add a web service reference to your console project for example. It will auto-generate the proxy classes for you to use in your code.

Webservices with dynamic urls

I have a project with several references to webservices and have made two different config files (one for dev/stage and one for production) with different URLs to the services. Whichever URL the webservices were originally added using works fine, but the other one fails.
For example, if I use the "add web reference" button to add a service at server1 everything works fine when I run my application. If I then switch out the config file so it's trying to use an identical service on server2 my application no longer works. Every call .net makes to the webservice returns null. If I use fiddler to see the XML the servers return they look identical.
Also, anytime the web services get rebuilt (even if nothing changes) I have to refresh all the references in my application. Don't know if that's related.
What would cause this and is there any way to fix it?
I'm doing a big project that has this.
A user creates an account with it's own web service endpoint, so I need them to be dynamic.
The trick is to always change the Endpoint address before each call to the service.
example of a Find service
FindClient wsFin = new FindClient();
wsFin.Endpoint.Address =
new System.ServiceModel.EndpointAddress(clientUrl.TrimEnd('/') + "/Find.svc");
just as a note: for WCF Services I have preferred to create a proxy using the svcutil instead of adding the reference, but for asmx web services, I did add reference :)

Consume web service in asp.net app from a class library

My team and I have a asp.net web forms application and are using several class libraries. In one of those libraries, we are trying to consume a web service. The web reference was added in the web app project and the appropriate references have been added. The app compiles. When attempting to consume said web service in the class library, the credentials don't seem to work, and the call fails. However, if we take the web service call out of the class library, and consume it within the web app, it works.
Any ideas why this is not working in the class library.
Double check your configuration file includes the correct information for the Web service.
Try changing the URL behavior to dynamic as well.
Also, as John stated, I'm assuming you're adding the service to the class library because you intend to use it from the library, as opposed to other areas of the Web application.
"the credentials don't seem to work, and the call fails"...can you give a small stack trace of the error?
Just to clarify, in my current project, we use WCF endpoints within a class library with bindings and credentials. The same can be done for a SOAP ASMX Web reference as you're attempting.
You can add a web service reference by doing the following steps:
right click on the project on the Solution Explorer
click Add Service Reference
click Advanced
you will find "Add Web Reference" at the end of the form
If you are adding the reference in application and then consuming it from class library... How you call the class library.. by adding reference and invoking the method of class library and then how you are accessing proxy from the class library you need to reference it... It seems to me a circular reference. Which shouldn't be compiled at first place... Are you describing your structure correctly???
It's always better to add a simple project with just web reference and then add the reference of this project on all the projects which requires it.
You can add a web service reference by doing the following steps:
right click on the project on the Solution Explorer
click Add Service Reference
click Advanced
you will find "Add Web Reference" at the end of the form
By #AMgdy 's solution,It'll auto generate a Reference.cs class.It defined all of method of webservices.
May be you called it wrong!!
Here is an example:
var serviceName = new ServiceName
{
Credentials = new NetworkCredential("Username", "Password", "Domain"),
Url = "Here you put the correct url of the web service if you published somewhere else"
};
serviceName.CallWebMethod();
make sure that you entered the correct Credential username and password and make sure the you published the webservice to a place you access it.
Have you defined any credential information in a config file in the web app? If so, the class library probably can't fetch them correctly. Just a guess though. And John Saunders is right. Seems a bit backwards reading your description of your apps structure.

Dynamic service reference in Silverlight

I'm building a Silverlight application that interfaces with SharePoint Web Services. In a windows forms application I'd create a web reference to my local SharePoint server, then change the Uri of the reference at runtime to point to whatever SharePoint site I wanted to use. Silverlight doesn't seem to have web references, but similar functionality can be achieved with service references. However, there doesn't seem to be a way to change the Uri of the reference at runtime. Is there a way to do this in Silverlight? Or a better way of using SharePoint web Services from Silverlight?
Note: I need to access list item attachments, so owssvr.dll won't be sufficient (I think?)
Seeing as you are using WCF (to reference the Service), you can change the Address of the service like so:
MyServiceSoapClient soapClient = new MyServiceSoapClient();
mySoapClient.Endpoint.Address = new EndpointAddress(URI]);
// do call here
There seems to be alot missing from this example. soapClient vs. mySoapClient?
Endpoint.address is not a property of a service (SVC).

Categories