Can a Web Reference Be Exposed from another Class? - c#

the question is simple..
we usually use web reference to consume the web services literally(visual studio). But, my problem is another way round. I need to call a particular web service from a c# class to make the web service available to be consumed by others.

First of all, if you have a choice, you should be using WCF on both the server and client. If you can't use it on the server for some reason, then at least use it on the client by using "Add Service Reference" instead of "Add Web Reference". "Add Web Reference" is part of the legacy ASMX technology, which should not be used for new development.
Secondly, of course you can create a separate class library, and use "Add Service Reference" in that library. You can then write a class that has public methods which call the service. The users of this class library will call your public methods, and will not directly call the service.

You can extract the C# code class of a Web Reference by unfolding the web reference in VS, (by showing hidden files if I remember well).
You should find a Reference.cs file which is the proxy client code for your web Service.
It contains everything you need to call your WS. You can copy it elsewhere, include it in another project, change its namespace/code etc. On the long run, maintainance of this file will be a pain if the corresponding WS evolves.
Anyway, if you have the choice, you should follow #JohnSaunders advice on WCF

Related

Confusion about WCF Class library and WCF application hosting

I have a web site and would like to expose certain functionalities using WCF.
Before deciding which type of WCF project I need to use I wanted to compare the differences between WCF Class library and WCF application. I know this question has been asked many times and answered many times and the answers are usually about different hosting options each one offers but I wanted to try and see the differences, so this is what I tried:
Step 1 - In a same solution, I have created a WCF Service library project and created Client console app project, set the console app as a startup project, referenced the service library project from the client project
and in the client console app I could instantiate the service and can consume the service methods. I didn't even add a service reference to the client project.
Step 2 - In a same solution, I have created a WCF Service application project and created Client console app project, set the console app as a startup project, referenced the service application project from the client project
and in the client console app I could instantiate the service and can consume the service methods. I didn't even add a service reference to the client project.
On both steps after compiling the solutions I was able to copy the client app's exe and the service dll's to a different location and still be able to run the clients.
Based on this little excercie I am confused about the hosting part. It seems wether I use WCF Class library or WCF application type I get the same result.
This is just like using multiple projects in a solution, you reference one from another and use the methods, there must be something I am missing which highlights the differences between the two and highlights the benefits of using WCF, also in the past I remember I had to add a service reference to the clients apps in order to consume the service, why is this not the case here?
Thanks
1) Running a wcf service application allows you to provide communication into a single application, where you have a single instance of a thing you want to provide access to. Maybe this is a game, or a chat room without an external state engine or datastore. This is useful for providing diagnostic information about an application you might have written for example. I used this to provide external control for an industrial robot that I wanted to provide remote control access for.
That is to say, that you write an application, it has a function. You want to expose part of that functionality to remote applications. You do this by adding a WCF endpoint to your existing application, so your application itself is controlling the WCF hosting elements, lifecycle of the endpoint etc.
2) Running a WCF Service is for when you've got an external data store, or your service is stateless. A translation service, lookup service and web page requests fall into this category.
With a service class, you're saying here is this service, this thing that provides a function. It isn't tied to the lifecycle of another application or process and is typically hosted by IIS. IIS manages when the class is loaded and run based on the requests that come into it. These services have no internal persistence and rely on an external datastore, or are, by nature, stateless (think of a postcode lookup, or a calculator service)
It sounds like you're actually adding the projects as references, rather than connecting to them as services. That is to say, that the consuming application is actually loading the service as an assembly (in the same application/ memory space) rather than as a separate application/ service that your application then uses WCF to communicate with.
The WCF Service Application template can be used to create WCF services with a hosting website created within the project
The WCF Service Library template can be used to create WCF services that are hosted by the WCF Service Host, and these can be tested using the WCF service Test Client.
the biggest advantage of using a standalone library (apart from decoupling the logic) is that you can easily migrate your service, i.e. host it in another application or another type of application. E.g., let's say you're hosting your service using IIS - you can easily move your service to a standalone application, etc.

Variable Service Reference Endpoint in C# and Visual Studio

Excuse my ignorance if this is something basic, I am somewhat new to the whole C# console application, using a Web Service Reference (WCF).
I am creating an application that will be places as an executable on many different machines and acts as a client to a web service. however, the web service is also installed in the client's local intranet, so the endpoint will change for each location.
I see the web reference endpoint is in the app.config file when adding within Visual Studio, but is there a simple way to change the endpoint at runtime, verify the endpoint is correct and exists, and be able to use the methods?
Also, if this is possible would I still need to add the SVC as a service reference in my Solution or would I call it dynamically?
I am using C# and .Net 4
Thanks
You can put the address as an AppSetting and set it after creating your proxy class:
Proxy.Endpoint.Address = new EndpointAddress("myconfigvaluehere");
First - there's excellent book about WCF services by Juval Lowy: Programming WCF Services: Mastering WCF and the Azure AppFabric Service Bus. He explains along the book about WCF wrapper class that connect to WCF service dynamically using Endpoint, Binding and all that jazz. But you need to understand WCF background to continue working with it <- and you can do it from the book :)

Reference C# Windows Service into an ASP.NET web application, is it possible?

I have developed a C# Windows Service application with certain methods that I would like to call from an ASP.NET web application (it is important that there are these methods returning the result). My initial guess was that I could import the dll of the Windows Service into the web application, but I can find only the executable of the windows service, there's no dll at all.
Is it possible to compile the Windows Service into a dll? And if not, is there some other way I can reference my Web Service into the Web application?
Extract the common functionality into a class library project and reference it from both projects.
Is your windows service hosting a web service or does it just contain code that needs to be accessible in you windows service and web site?
If the former, then you can connect to it via the normal mechanims for connecting to a web service (e.g. adding a service reference in Visual Studio). If the later, then create a class library that both your windows service project and web project can reference (via Add Reference in Visual Studio).
You have two options. If you just need the code to be invoked, then you can call the windows service functionality via a custom action on the service, invoked from your code.
However, I gather that what you require is more complex, requiring a response as well. In this case, as per sga101, encapsulate the appropriate functionality into a separate class, and use this in both places. This means that the code projects should both be included in the same solution ( it is not strictly necessary, but it makes it easiest to keep the code matching ).

C#: Webservice changes expected parameter type (from a normal POCO to a an autogenerated class)

I have the following class in Class Library: Artist, which is a POCO
Now I have a method in a web-service (which has a reference to the mentioned-above library) with a signature like this:
[WebMethod]
public int Artist_AddArtist(Artist a) {
//
}
When I try to consume this service from an application (that also has a reference to the mentioned-above Class library), the expected parameter of the Artist_AddArtist method is not Artist, but a new type of Artist that is being generated in Reference.cs which is a partial class that is auto-generated.
Thus since in my application I use, supposedly the same Artist class from the library and now the Web Service method expects this new auto generated type, I cannot pass an instance of it to the web-service.
How can I fix this issue?
Maybe switching to WCF services is an option for you. As far as I remember, with a WCF service, you can reuse the same types on ther server and client side.
This article explains how to migrate an ASMX web service to a WCF service.
You cannot, and should not, fix the problem.
Some others will tell you to do things like edit the generated file, but that's not a good practice (as the changes will go away as soon as the Web Reference is updated).
What you're seeing is by design. See Basics: How Web Services Work.
Briefly, when you use "Add Web Reference", Visual Studio downloads the WSDL file from the service, and uses the XML Schemas from the WSDL to create some proxy classes to represent the XML described by the schema. It also creates a proxy class for the service itself, having methods for each operation in the service.
The proxy data classes can serialize to the XML that the service is expecting to receive, and can be deserialized back from the XML that the server sends in reply.
One way to think of it is that you only have this problem because both client and service are .NET. If your client were written in Java, then you wouldn't be thinking of sharing classes.
Note that WCF can do this, if necessary. It introduces a dependency between the client and service (they both have to use compatible versions of the assembly containing the classes), but when you need to do it, the option is there. It's useful when there is behavior in these classes that must be used both by the client and by the service.

Calling ASP.net Web Service from C# Application

I have a question. How can i invoke a web service and get the result from a C# desktop application. I am making a desktop app and I want it to be able to connect to my online ASP.net web services. How is this possible?
In Solution Explorer, right-click your project node and select Add Service Reference.
Enter the URL where your service WSDL is located. This is usually the URL of the service itself.
This generates a strongly-typed proxy class in a new Services References folder in your project.
Write code in your desktop app to instantiate the proxy class and invoke methods on it. The rest works like magic. :)
AB Kolan was also correct, but Add Web Reference uses the old-style web services framework whereas Add Service References uses the new WCF stack. Important note: It is not required that the service itself use WCF for you to use WCF on the client side. WCF on the client is typically the best choice for any service, provided you can take a dependency on .NET 3.0 and above.
Add a Web Reference to the webservice in your Desktop App project reference. Doing so would generate a Proxy for the Webservice called Reference.cs
You can access your webservice using the proxy.
This is possible the same way that you access web services from any other type of application, be it an ASP.NET page, a class library or windows service.
For an explanatory tutorial on the subject, see Accessing a Web Service from a Desktop Application.
Will get help how to create a webservice and consume that service:
http://www.c-sharpcorner.com/UploadFile/0c1bb2/consuming-web-service-in-Asp-Net-web-application/
Thanks

Categories

Resources