C# WSDL proxy methods - c#

I need to consume a SOAP web service from a C# .Net 4.0 client. I've added the WSDL as a Service Reference in VS 2008 and can now see the XML types as classes so I can construct the SOAP message.
The WSDL Port is defined as an Interface and the two operations defined in the WSDL (UploadMessage and ValidateMessage) are visible methods.
How do you actually use these methods? I thought the Port would be available as a class so you could directly call the methods and that VS would have created the relevant code for sending the SOAP message, but it looks like you have to write code to go in these methods. Is that correct?
I have googled this but can't find a simple example using .Net 4.0 ;-(

When you add your service using Add Service Reference, you should have a new namespace (which you defined in the Add Service Reference dialog box - bottom left corner).
Inside that namespace, there ought to be a class called something like (yourservicename)Client.
If you can't find it, open up the Service References node in your Visual Studio Solution Explorer, and find the Show All Files button in the Solution Explorer's little toolbar - click on it to see all files.
Under your Service Reference, several nodes deep, you should find a file called Reference.cs which contains the code classes generated from your WSDL.
In that Reference.cs, you should find your client class. This is a regular class, which you can instantiate in your code and call methods on. This will call the web service methods on that remote server you want to connect to.

Typically a Service Reference will create a class called [ServiceName]Client that implements the interface you mentioned.
New it up, then call the operations.
Of far greater importance than getting the client and calling it will be your configuration, which will be dependent on the service. This article will help.

Related

WCF - regenerate client code from interface contract

I have quite simple WCF service and its client - both reside in my application.
At the moment interface used for WCF contract consist of 3 methods, and according to MSDN I generated client code for it using svcutil.exe CLI.
But now I added one more method. And I wonder - is there a simpler way to regenerate client code (probably directly in VisualStudio) using doing less actions for this operation?
Right click on your Services References and select the service you'd like to regenerate code for. Then click Update Service Reference. Then you're done.
Update after re-reading the question I realized its possible you don't know how to import WCF Services.
Here is how its done:
Update as per comments:
There are two workarounds for this, The first is to run the application outside of your IDE and then go into your IDE to import it.
The second one is to create a proxy client that implements your service contract. When you are using the client proxy you can right click on the interface and select an option to implement the interface. Assuming that the set up is done correctly, if you make a change to the ServiceContract you can then go to your proxy class and just click implement interface which will update your proxy class. by doing this you bypass the svc utility completely; however, you have significantly more control over your development. You need to work with DLLs to accomplish this; however, it works like a charm. Here is an example though I doubt you need one I'll just leave it for the sake of completeness. Use this example for duplexes

How to read Web Method and parameters name from Web Service

I am C#.NET developer. I am writing a application which can call web service dynamically. I got one web service that is written in Java. This web service returns only web method name in WSDL file but doesn't returns their input/output parameters details. They keep all details in another XSD link. That xsd link present in WSDL file. I want to know that how I can read that XSD file and how we can read Web Method name and their input and output parameters.
I am looking help on priority.
this is sample link: http://www.java-tutorial.ch/api-doc/ArticleService.html#src.N10068
Thanks in advance.
Rajeev
XSD files are valid XML which means you can parse it with classes from the System.Xml or System.Xml.Linq namespaces. I'm assuming when you say you're dynamically calling the web service that you can't rely on a static WSDL reference in Visual Studio
Assuming you are using Visual Studio and are looking to generate proxy classes to consume the web service you need to add a web reference to the WSDL. You can find instructions on how to do this at microsoft.
This process will generate the classes required to interact with the webservice, including all the methods and properties.
This isn't the only way to generate the proxy classes, but IMHO it's the easiest.
Here is a working example of what you can end up with:
I develop against an ERP system called Netsuite. They provide a WSDL at https://webservices.netsuite.com/wsdl/v2013_2_0/netsuite.wsdl.
Following the instructions in the link above I show the following:
I can now call the class like any other. In order to view the classes now available,
view the reference in the Object Browser. The image below shows all the classes, each one of course you can click into to see what properties are available.

Portable class library for shared [DataContract] class

This is probably a real simple one. I wished to create some code inside a [DataContract] class which is used to communicate information between a WCF service and consuming application.
I created a new Portable Class Library project and created inside it my [DataContract] class. It's really simple - it has a few easy attributes (strings mostly). I added a reference to the DLL in my WCF service and republished it. I then deleted the service reference from my consumer project and re-added it. It adds fine, no problems there.
The problem comes when I want to actually use it. See, the type is called 'Eval'. Some of the WCF methods return this 'Eval' type. So I have this serialized version of 'Eval' in the Reference.cs file within my service reference. Obviously this doesn't contain the method I'm looking for since it's just the serialized version. I can add the dll to the client project as well, but then I have two different Eval objects in two namespaces, and only the one from reference.cs is valid to be returned from the service calls.
So how do I ... erm... merge the two? So as the Eval objects that come back from the WCF service calls also have access to the method in question?
Thanks for reading, and stay safe on those roads.
When generating service reference you can specify what classes should be reused and what classes should be generated from service

Adding WCF Service References to multiple projects

A couple of very basic questions. I am new to WCF and I am building an application which has a Service Project, A Web Application project and a few Class library projects which I use for Business logic, etc.
I am hosting the WCF locally on my IIS and trying to add service references to the projects.
Question 1. When adding references, should I add a service reference to each project separately or is there a way I can share the same Service reference across the projects?
The reason I ask is because if I add separate references, each reference gets it own namespace, and when I have to pass the same object between the projects, I get an InvalidCastException because each ServiceClient has a different namespace.
Example -
Site.Business.XDataService.XDataServiceClient().GetItem()
is not the same as
Site.Web.XDataService.XDataServiceClient().GetItem()
Question 2. I specified the address of the local service in the class that implements the Service interface as below -
[ServiceBehavior(Namespace = "http://localhost:801/XDataService.svc", IncludeExceptionDetailInFaults = true)]
This doesn't seem right. If I move my code to a different/live environment, I would obviously have to change this part again and recompile. Where can I specify this (Web.Config?) so that I can change this address without having to rebuild my app?
Appreciate any kind of insight.
Thanks!
In answer to the first question, you can put the service reference in its own project and reference that project in all the other projects that need to access that service.
Basically all the service reference is is a lump of .NET code - namespace, class, etc.
Better yet (!) for a WCF service you also get an interface thrown in for free (more or less the same interface that you defined for your service) so you can do nice things in terms of dependency injection making testing etc easier.
First question - the service is just like any other code. For example, database access code. Should you put that in every project that needs to access your database? No - you should put it in a project which those other projects can reference.
As for your second question, you're specifying a namespace but I expect you think you're specifying a service endpoint address. The namespace is just like a C# code namespace - it essentially provides further identification and clarity in the event that you have multiple objects with the same name. Normally you'd use a namespace like http://mywebsite.com/MyService/VersionNumberIfRequired or similar.
The address itself is specified in configuration. The address will change depending on environment / deployment location - the namespace shouldn't.

How to call a webservice in the same website?

I have to call a webservice published in the same website the caller aspx is.
When I try to "Add a Web Reference" the editor does not show the webservice methods from the generated namespace.
Do I have to use the "Add Web Reference" or is there another way because the webservice is in the same website?
Visual Studio 2005, C#
Thanks,
Eduardo
I used the way that Andy Rose wrote in comments:
Instantiate the webservice class and call the methods directly because it is accessible inside the project (no need to add web reference).
There are ways to trick it... You can deploy the app with the web service, then add reference to the deployed location.
Another idea is to just start it using ctrl-f5 so as to not start the debugger as well, then add a reference to your localhost:/.asmx
This will get all of the configuration information created in your web.config which, of course, you can modify later as necessary.
The Webreference itself is just a proxy implementation so that VS can pretend it knows how your webservice will react to calls to it (so you can compile), you can actually code without them (though it is much harder :)).
You have a few of options, that I can think of, if you want to do this:
Create your webservice as a separate project, but deploy it to the same location. You will get mixed binaries in the bin directory, but otherwise it should work fine. In this way you can deploy each separately as needed.
Consider pulling them apart into two separate applications. This might not be available for whatever reason.
Create a stub webservice in your main project, which only has the function definitions (return type and parameters) and deploy that, then generate your webservice against that. You can then begin your development against the prototype and fill it in as necessary
Do the two-step shuffle as Chris suggested. First create a approximate representation of your webservice, deploy it. Second create your proxy against it, create your web app, deploy it and test. Repeat as necessary.
Once your proxy is created you can change the URL it points to in the proxy bindings.

Categories

Resources