Kairan
Kairan

Reputation: 5542

How to send data to and from ASP.NET 4.0 website and C# WCF (Web Service)

I am wondering - in my ASP.NET 4.0 website how to connect to the web service I have running?

This is essentially what I would like to accomplish, for testing before I apply it on a larger scale. I want the website to be able to send a value (int) from the website to the getData() method in the web service (auto generated method) and return its result (string) back to the website.

This is my setup for the Website/IIS 7/WCF:

Steps for creating website and service via VS 2010 (.NET 4.0) Website:

File > New Website
I did not change anything on the default website template
Build > Publish Web Site
  Set target location to specific folder
  Other settings unchanged

IIS Settings:

Connections - Right Click > Add Web Site
  Specified a site name
  Specified physical path
  Connect As:  Set the username/password
  Binding Default (http port 80)
Connections - Right Click on Default Web Site > Manage Web Site > Stop
...So as not to end up with two websites on conflicting port
Edit Permissions > Add user "Everyone"
MyNewSite > Manage Website > Start
...Clicked Browse Web Site > Brows *.80 (http)

And the website ran just fine, I was able to access it locally and through vpn with the IP address.

WCFService:

File > New Project > WCF Service Application
...I did not change any of the default settings in the WCF service

I ran WCF service as Admin, and it loaded the little test app where you can type in a value to the getData() method and it returns "You typed {some_number}".

So as far as I know - everything is working with the website and the wcf service. I tried also right clicking on the website in VS, and Add Service Reference and it was able to detect the WCF service.... however, I dont know that I should need to add a reference but simply connect to the WCF like I do to a database via a connection string of sorts.

Upvotes: 0

Views: 1168

Answers (1)

Tim
Tim

Reputation: 28530

To elaborate upon my comment, here's a stripped down example. You can find numerous examples of how to do this with a very simple Google search, by the way.

In order for your web site to call any of the methods, it must have a WCF client proxy. This proxy is used to establish the connection to the service and make calls to the methods the service exposes via the [OperationContract] attribute.

Taking your posted example above, if you created a basic no frills don't touch anything WCF Service Application, and you add a reference to it in your website from within Visual Studio, the WCF client proxy will be generated for you, as well as any necessary configuration file settings (in your Web.config file).

Once you've added the reference, you can then do something like the following in your website:

ServiceClient client = new ServiceClient();

string serviceResponse = client.GetData(5);

client.Close();

This would return "You entered: 5" for serviceResponse.

Alternatively, you could use the svcutil.exe to generate a *.cs file and and a config file. You would add the *.cs file to your website project and the <system.serviceModel> section of the config file to your Web.config, and then could use the above code to call the service.

A third way, and this is the way I prefer because of the nature of our application at work and it gives me more control, is to use ChannelFactory<T>, but I would suggest trying the above two approaches to get familiar with the basics first.

Again, see Accessing Services Using a WCF Client, and if that isn't sufficient do a Google search for more examples.

The key point here is that the client requires a proxy to communicate with the WCF service. How you generate that proxy is up to you and the requirements of your project.

Upvotes: 1

Related Questions