Geeks With Blogs
Random Musing Putting it together one piece at a time...

Below is a simple and straight forward tutorial on how you can get your web application to call a WCF service hosting on IIS6. I've broken the tutorial down into steps so as to faciliate a hands-on walkthrough.

Step 1
Create your WCF service
First of all, create your WCF service by specifying the interface as well as the implementor

//define the interface
public interface IWCFService
{
         [OperationContract]
         string myString(string input);
}

//implementation of interface
public class WCFService : IWCFService
{
          public string myString(string input)
          {
                 string output = input;
                 return output; //simply returning the input as output again 
          }
}

Step 2
Create your WCF service's web.config
You will need to create the web.config file for your WCF service to work. For WCF we can either dynamically programming these settings at code level or do it in the configuration. For this tutorial we'll do it in the configuration file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>

<services>
<service
name="WCFService.WCFService"
behaviorConfiguration="WCFServiceBehavior">
<endpoint address=""
binding="wsDualHttpBinding"
contract="WCFService.IWCFService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>

<behaviors>
<serviceBehaviors>
<behavior name="WCFServiceBehavior">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>

</system.serviceModel>
</configuration>

Step 3
Create your svc file
The svc file is required if you're hosting your service in IIS. This will not be required if you're hosting it in another application. The code base is however the same regardless of the chosen host method. Create a new file and give it a .svc extension. Paste the following code into the file

%@ServiceHost language=c# Debug="true" Service="WCFService.WCFService" %

Step 4
Host it in IIS
Now create a virtual folder in IIS and copy the compiled .dll into the bin directory, and copy the .svc file and web.config file into the virtual directory. Remember to change the .Net framework into 2.0.

Step 5
Create the web application client.
Now you can start to create your web application. Create a simple web application. Go to Windows SDK cmd and execute the following command

svcutil.exe http://localhost/wcfservice/interface.svc /out: proxy.cs

This will create the proxy and web.config file that is required for your web application to call the WCF service hosted on http://localhost/wcfservice/interface.svc

Step 6
Write the code to call the WCF service in your webpage
Open your default.aspx, copy the proxy.cs files into the App_Code folder and call the WCF service

using (WCFServiceClient client = new WCFServiceClient())
{
        string output = client.myString("myinput");
}

Step 7
Merge the generated .config file into the web application web.config file.
Now simply merge the auto generated configuration file into the web application's web.config file.

<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IOperationalService" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384"/>
<reliableSession ordered="true" inactivityTimeout="00:10:00"/>
<security mode="Message">
<message clientCredentialType="Windows" negotiateServiceCredential="true" algorithmSuite="Default"/>
</security>
</binding>
</wsDualHttpBinding>
</bindings>


<client>
<endpoint address="
http://dms_poc/operationalservice/interface.svc" binding="wsDualHttpBinding" bindingConfiguration="WSDualHttpBinding_IOperationalService" contract="OperationalService_Common.IOperationalService" name="WSDualHttpBinding_IOperationalService">
<identity>
<servicePrincipalName value="host/dms_poc"/>
</identity>
</endpoint>
</client>
</system.serviceModel>

Step 8
Test your application.
Test the application by adding any simple text control which will show the return results and a command button to trigger the call.

 

I hope this simple tutorial will come across as useful to you.

Posted on Tuesday, March 6, 2007 10:18 AM | Back to top


Comments on this post: Web application calling WCF services

# re: Web application calling WCF services
Requesting Gravatar...
This is really helpful, is there any way to send xml file from windows service to web application

language = c#
Prtocol = HTTP
Communication point = SSL

if it is possible plz help me
Left by dhakshin on Sep 22, 2007 6:10 AM

# re: Web application calling WCF services
Requesting Gravatar...
thanks for ur reply but my requirement i have to send xml file from windows service to web application. help me thanks in advance
Left by dhakshin on Sep 26, 2007 3:10 AM

# re: Web application calling WCF services
Requesting Gravatar...
I'm glad the articule helped you!
Left by Nestor on Jun 15, 2008 3:17 AM

# re: Web application calling WCF services
Requesting Gravatar...
This is good stuff, but if this is replacing simple web services, why is it so complicated. I am running in an n-tier environment with mirrored front-end servers and mirrored web servers. I don't have the ability to create virtual directories on the web servers, just locally and then have to push my web service out to these servers.

Any suggestions on how to do that?
Left by Randy Schmidt on Aug 09, 2008 4:05 AM

# re: Web application calling WCF services
Requesting Gravatar...
Really very nice article. it is describes as so simple. Thanks a lot
Left by Debasmit on Nov 17, 2008 12:59 PM

# re: Web application calling WCF services
Requesting Gravatar...
svcutil.exe http://localhost/wcfservice/interface.svc /out: proxy.cs

Here we are talking about one single service contract implementation, let's say IService1.cs

I wonder if we keep creating separate .svc for each of such 10-15 contracts/implementations.
And also create 10-15 such proxy classes.

And add all of these .cs files in my client application. There should be some better way to architect this.
Left by Chirag on Jan 09, 2009 3:04 AM

# re: Web application calling WCF services
Requesting Gravatar...
Nice Article dude.Was very helpful
Left by George on Apr 27, 2009 3:07 PM

# re: Web application calling WCF services
Requesting Gravatar...
Really helpful for bigner ;) Much appreciated.
Left by Mohamed Harris on Jun 14, 2009 7:08 AM

# re: Web application calling WCF services
Requesting Gravatar...
Hello,
i want to ask about step 6.
can we web host name and web service to be used at run time here while creating web service class object.
thanks
Left by Pratiksha on Nov 11, 2009 7:20 AM

# re: Web application calling WCF services
Requesting Gravatar...
svcutil seems flaky - following your instructions (and other similar walkthroughs) I can't seem to get a WCF client app to talk to a WCF service. The service rejects the utf-8 message, and says there must be a configuration difference between client/server. Well, if there is, svcutil created it! Also, ASP.net clients are refused by a WCF service, since they use the connection string "Mozilla" - as do most web browsers, including I.E. This all seems a lot more fragile than it should be... Do you have any hints or suggestions?
Left by Cameron on Dec 08, 2009 1:46 PM

# re: Web application calling WCF services
Requesting Gravatar...
Hi,

this article is really very very helpful.
I have one question .

can i consume the WCF service without creating proxy, and adding web reference just like we add for the reguler .net webservice and then call the method by creating the instance..
Left by Shilp on Apr 06, 2010 1:10 PM

# re: Web application calling WCF services
Requesting Gravatar...
i have an error which is define as below .
System.Net.WebException: The underlying connection was closed: The connection was closed unexpectedly.
Left by radhika on Aug 06, 2010 1:09 AM

# re: Web application calling WCF services
Requesting Gravatar...
I got erro which is "the type or namespace anme 'WCFServiceClient' could not be found. Would you tell me how to solve this problem
Left by BMW on Sep 24, 2010 12:04 PM

# re: Web application calling WCF services
Requesting Gravatar...
Good Material
Left by Manas on Feb 12, 2011 3:30 AM

# Web application calling WCF services
Requesting Gravatar...
This is nice article and helped me...

Thanks...
Left by Manikandan on Mar 10, 2011 12:52 AM

# re: Web application calling WCF services
Requesting Gravatar...
you r not declare where add the text box and button field
Left by vipul on May 03, 2011 3:29 AM

# re: Web application calling WCF services
Requesting Gravatar...
Thanks buddy, Simply superb example.

Left by Suresh on Jul 13, 2011 1:52 AM

# re: Web application calling WCF services
Requesting Gravatar...
Its so highly informative things are posted here. These things are the fresh and having good information are posted here, and also am seeking for this kind of information thanks for updated.
business coaching Brisbane .
Left by business coaching Brisbane on Jul 13, 2011 2:23 AM

# re: Web application calling WCF services
Requesting Gravatar...
thanks a lot...its working
Left by varun mohan on Jul 19, 2011 7:09 AM

# re: Web application calling WCF services
Requesting Gravatar...
thanks a lot.......its working....
Left by varun mohan on Jul 19, 2011 7:11 AM

# re: Web application calling WCF services
Requesting Gravatar...
This is very useful to me as Istarted learning the WCF usage in web applications
Left by Praveen on Jul 23, 2011 10:40 PM

# re: Web application calling WCF services
Requesting Gravatar...
the content was good but u would have provided the code in a gud manner rather than in a complicated way
Left by neeraj on Sep 19, 2011 3:46 AM

# re: Web application calling WCF services
Requesting Gravatar...
its good
Left by nike on Sep 19, 2011 4:36 AM

# re: Web application calling WCF services
Requesting Gravatar...
Hello! Thanks for posting this tutorial.

You have mentioned at step 2
for WCF service to work we can either dynamically programming the settings at code level or do it in the configuration.

Do you have a tutorial that explains how to do it a code level and get rid of the web.config for storing httpbindings and endpoint settings.

Thanks in advance.
Left by Mithun on Oct 10, 2011 2:06 AM

# re: Web application calling WCF services
Requesting Gravatar...
Great Article.
I hope this is helpful for all my developer friends
Left by Deepak Kataria on Nov 24, 2011 12:11 AM

# re: Web application calling WCF services
Requesting Gravatar...
i cannot implement what he is saying can anyone help me step by step so that i can work on it my question is how to create a service with binding property we need to set for string and all data types and i followed it still it's not working
Left by Karthik on Mar 22, 2012 8:53 AM

# Web application calling WCF services
Requesting Gravatar...
Nice Article.If you want to get more example of wcf web service with example then use this link this link is very helpful for making the web service using wcf restful and normal web service
http://www.dotnetnukes.blogspot.in/2013/04/create-restful-wcf-service-api-using.html
Left by chitranjan on Apr 23, 2013 7:00 PM

# re: Web application calling WCF services
Requesting Gravatar...
Nice article..

But I have found another simple example to call a WCF RestFul Service using jQuery AJAX Call, please refer to link below:-
http://www.etechpulse.com/2014/03/consume-restful-wcf-service-in-aspnet.html
http://www.etechpulse.com/2014/03/how-to-consume-wcf-restful-service-in.html

Thanks
Left by Rk on Mar 06, 2014 1:16 AM

# re: Web application calling WCF services
Requesting Gravatar...
This is very nice post.If you want to get more related to asp dotnet web service example then use this link
http://dotnethubs.blogspot.in/2013/08/how-to-create-json-web-service-in-aspnet.html
http://dotnethubs.blogspot.in/2013/08/creating-ajax-enabled-wcf-service-in.html
Left by ravi on May 02, 2014 12:08 AM

Your comment:
 (will show your gravatar)


Copyright © Nestor | Powered by: GeeksWithBlogs.net