WCF Simple Example

You may also like...

160 Responses

  1. Ralf says:

    If I inspired you to boil down your previous code to come up with an even simpler scenario to introduce WCF, I´m happy. Maybe we should start a competition on how few lines are necessary to use get started with WCF? ;-)

    However, you mentioned twice you wanted to abstain from commenting my sample. Why? What´s wrong with it? I don´t think using two processes is necessary for a WCF introduction. It just makes it more difficult to get the whole thing running. WCF makes process boundaries pretty transparent, so I don´t think they are needed.

    Rather much more important I deem separating contract from implementation – which you neglect. Also a WinForms application does not make WCF easier to understand.

    Also, do you think meta data exchange makes understanding WCF easier? Hm…

    But anyway, I´m happy you took up the challenge ;-)

    Cheers

    Ralf

  2. Hi Ralf,

    The single project to get everything going isn’t what WCF is about, I think. So that’s why I always need at least two projects.

    The thing I ment was you were creating client and service by means of multi threading. That doesn’t make the example easier to understand, just a lot harder.

    The other thing about process boundaries. I’m not sure what you mean there. With WCF you ALWAYS have two process boundaries, because your service/host and client aren’t on the same process. In WCF vNext that might be possible, but currently it isn’t. Even though you only have 1 project and start everything on multiple threads, still everything is serialized into XML and send cross process boundaries.

  3. Ralf says:

    Hi, Dennis,

    I understand what you say, you need two projects. And I agree in the end WCF mostly will be used in such scenarios. But two projects are less easy to set up in VS than one ;-) That´s the only reason I used one project or even just one file. I´m just concerned about getting a WCF sample up and running with the least number of steps and with the least number of files. So I guess we can agree that this is possible only with multithreading like I did it ;-)

    In addition, though, I think, WCF is not about a particular “distance” of client and service. It´s perfectly fine to use WCF within the same process, e.g. to cross AppDomain boundaries. And it´s a means to hide “distance” in general so I don´t have to care whether code is running in the same or a different AppDomain or in another process or on another machine. So letting two threads communicate using WCF for me is perfectly valid – albeit maybe a little bit unusual.

    What you say about process boundaries does not seem 100% correct to me. You´re right in that messages are always passed between client and service (host). Data always will be serialized. But a process boundary – as you can see in my example – is not necessarily crossed. My client and service host live in the same process and that´s just fine with WCF. However, in a larger scenario I would not just provide a TCP endpoint for intranet communication but also a named pipe endpoint for fastest communication on a single machine – possibly within the same process.

    But again: I was not concerned with a real life example of WCF usage but a most easy demonstration.

    -Ralf

  4. Just like to say thanks for a great article. I just started looking at WCF today, and your article has really helped me get started.

  5. Earle Beach says:

    Thanks a lot! After crashing and burning with MS’s “Technology Samples,” it’s really a great feeling to be able to build a sample that works the first time.

  6. dylan H says:

    Hi,

    I just wanted to thank both Dennis and Ralph for your examples. By attempting to create simple example in different ways, you appeal to varied audiences. Looking at both of your examples will has help get closer to delima I am faced with with.

    Thanks

  7. David T. says:

    I wouldn’t normally make a WCF project as only one project, so the example of two projects is useful. However, Ralf’s example showed how it could be done in a multi-threaded way – something I would not have known to do. Thanks to both of you.

  8. Sanjay says:

    Hi Dennis,

    Thanks for ur example.

    My quest for understanding WCF finally ended by ur example.

  9. Sivakumar says:

    Very nice example with explanation

  10. Meher says:

    Hi… Dennis

    I started 3.0 recently and was confused how to start the WCF. Your article is so helpful

    Thanks

  11. Pasquale says:

    I couldn’t use the example until I had installed not only Visual Studio 2005 and .Net Framework 3.0, but also the “Windows SDK” for .Net (Vista version) and most importantly “Visual Studio 2005 extensions for .NET Framework 3.0″ available at: http://www.microsoft.com/downloads/details.aspx?FamilyId=5D61409E-1FA3-48CF-8023-E8F38E709BA6&displaylang=en

    Neither this example nor anywhere else I looked explained that.

    I can’t help wondering where the world will be in 5 years when everyone needs to upgrade from WCF and we are all captive the black magic of this technology.

  12. @Pasquale : Just updated the article, you’re right, it should’ve been in there. Other people can thank you for it now. :)

    However, you don’t need the SDK and your link is to the WF extensions, not the WCF/WPF extensions.

  13. Pasquale says:

    Right, I’m told both WF and WCF/WPF extensions are needed. Here’s the link to the WCF/WPF extensions:
    http://www.microsoft.com/downloads/details.aspx?FamilyId=F54F5537-CC86-4BF5-AE44-F5A1E805680D&displaylang=en

  14. SK says:

    Great, Just one word.

    ThankYou

  15. kenny says:

    Thank your example, it is pretty helpful and clear to understand the WCF structure in inline code.
    you are right, To separate the host and client to different projects makes the route and the concept much more clear, even I modified the binding address from ‘localhost’ to other ‘192.168.xxx.xxx’ and run the service on other machine, it will be better.
    one thing need notifiy is service reference name ‘localhost’ would be customized to other name by changing the reference name or in its property, it made me confuse at the begining.
    Do you have other example using IIS to host the service,config and expose it in web.config? I would like to enjoy it.

  16. Kenny, good idea! I’ll create an example and tutorial for that. But it’ll probably be next week.

  17. Alan says:

    One or Two project samples are nice so long as it’s short and simple like this one and i don’t see what the fuss is about.

  18. Alan, what do you mean with ‘the fuss’?

  19. Waqt says:

    Good one it helped me a lot thanx

    but can u provide the steps for WCFClassLibrary also

  20. newbie says:

    Nice sample!!! yes this is what i have been looking for too, nice and simple. would you happen to have a WF State machine simple sample too?

  21. jeff says:

    Trying to use this with VS 2008 and I cant seem to set the ref to the host service while it is running. Any ideas? All the options on the Client project are grayed out when the host is running.

  22. Great article. I like simple.

  23. Sujith says:

    Great one, It was really helpfull

  24. David Rogers says:

    Nice simple article. However, I ran into a gotcha immediately:

    “HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).”

    Apparently, metadataexchange requires elevated privileges? Is this something I am getting because this is VS2008? Vista Ultimate? Any ideas on how to be able to tell Visual studio to debug with elevated privileges?

    Thanks,
    David

  25. Amit Dave says:

    Hi
    First of all would like to thank you for such an encouragin example;

    and am sure this is just a begining would like to have more detail and indepth use of WCF, could you just let us know that how wsdl and wcf are inter-related with each other and more inportantly scenarios suitable for WCF…

    thanks again and expect such articles in future as well…

  26. Dennis van der Stelt says:

    @David : It’s a Vista ‘problem’. Run VS2008 as administrator (which I always do), or check out this article : http://blogs.msdn.com/paulwh/archive/2007/05/04/addressaccessdeniedexception-http-could-not-register-url-http-8080.aspx

    @Amit : There are more articles on my weblog, just click the “WCF” tag in the tag-cloud.

    I’ll make a post about WSDL and MetadataExchange in the future to try and explain it the simple way! :)

    For now I can say the “WSDL” is the “WebService Description Language”. You can enable it via the metadata service behavior. WSDL is like an “addon” feature on MetadataExchange so that consumers (clients) that don’t support metadataexchange are still able to retrieve the WSDL.

  27. chandru says:

    Thank you very much.Its really useful for beginners in WCF.This real time exmaple helped me to understand better.

  28. Venkat R Y says:

    I am new to WCF this example has helped to understand how WCF works… Thanks very much Dennies.

  29. prabhu says:

    Thank you, Simple example has helped lot to understand WCF

  30. Aravinthan says:

    Hi Dennis,
    Thanks a million. I am a newbie to the WCF and simply couldn’t get a clue of how and where to start. Your simple example gave me a solid starting point. Once again.. thanks a lot and keep up the good work.

  31. abraham says:

    Dennis,

    Your’s was the first WCF example that I was able to get working successfully, Thank you.

    I have been trying one example after another in the hope that I could better wrap my head around WCF concepts once I had a working model to play with.

    Your example creates a temporary web service. How does one go about deploying the service to IIS 5 & 6?

    Appreciate the hand-holding.

  32. Kenneth says:

    Thanks for the great example. But I wonder if you can tell me why my service reference will not update. I have changed the endpoint addresses to a foreign machine’s address and I can view the output in a browser when I navigate to that ip. However, when I try to do “Update Service Reference,” it errors out. It is not a firewall issue as I can browse to the machine, but what else could it be?

    Thanks.

  33. sandhya says:

    Really this is very nice example for those who started 3.0

    can u please give some more examples

    thanks and regards
    sandhya

  34. Dennis van der Stelt says:

    @abraham: I’ll try and post something soon about that.

    @Kenneth: Normally you can only see the nice HTML page when going to the service when you’re browsing locally on the machine (or server). Maybe you don’t have a MEX endpoint on the server so it can’t update?
    Either way, when from development the service its interface hasn’t changed, you should be able to change the url of the service in the web.config or app.config and be fine. You don’t have to actually update the service reference, that’s just for updating the underlying proxy code, for when you’ve changed your interface (ie. added new operations, changed parameters, etc).

  35. Alex says:

    thank you, very helpful article !

  36. Suresh says:

    Suresh …. where’s the Choclate?

  37. Pooja says:

    HTTP could not register URL http://+:8080/ because TCP port 8080 is being used by another application.
    I am getting this exception for each and every WCF example that I try to run.
    I am a student and rwally need somebodys help.
    Please tell me how to go about it in detail since I am a student I really understand only if told me in detail please.
    Thanks

  38. Dennis van der Stelt says:

    @Pooja : That’s the problem with students, if you don’t tell them in detail, they won’t know. That’s not what we need at companies! Nah, just kidding! ;)

    Anyway, only one application can host something at a specific port. So if some other tool is using 8080, use a different port. You can also use 81, or 8081 or any number. I also like port 1337 :)

    It’s also possible that Vista doesn’t allow you to open the port and that VS2008 (or .NET actually) reports a strange message. Are you running Vista? And if so, are you running VS2008 in Administrator mode?

  39. abraham says:

    “@abraham: I’ll try and post something soon about that.”

    Just a reminder if you can make the time.

    I admit once I got your example working I was able to implement a nice prototype that involves queuing messages on MSMQ and processing them asynchronously.

    I still have run into trouble deploying to IIS however :(

    Thanks for getting us all started.

  40. Dennis van der Stelt says:

    @abraham: You’re right, and promised is promised. I’ll set a reminder in Outlook

  41. Jeff says:

    Excellent example for demonstrating the basics of WCF. Just what I needed – thanks!

  42. GiulianoMX says:

    Great article, thanks for taking your time an putting it together.

  43. darge says:

    I am using visual studio 08

    I created the host and tried to test it using IE7 but it returns an error.

    any solution……

  44. Dennis van der Stelt says:

    darge : You seriously can’t expect me to help you with so little information.

  45. Ravi Vaswai says:

    Nice Article..Thanks Dennis van der Stelt

  46. Sunil Kurian says:

    Thanks Dennis,

    This is a very cute and rich example to start with. Expect more examples to lead us to grow better on different scenarios.

    Really appreciate for what you deliver.

    regards
    Sunil Kurian

  47. Ran says:

    Thanks !

    This help to understand

  48. Vaishnavi says:

    All the steps till 8 were successful however I dont find localhost.map and localhost.cs file under Service Reference. app.config is available though. I am stuck with the button event coz of that.

    I am using VS 2008. I could see those files in your downloaded code after successfully opening in VS 2008.

    Please help.

  49. Dennis van der Stelt says:

    @Vaishnavi : Were you able to create the “Service Reference” in VS2008 in your client project? ie Client Project being the project that would call the service?

  50. Sujith Balakrishnan says:

    Cooollllllllllllllll

  51. Balazs says:

    I had the same problem in VS2008 as Vaishnavi.
    I were able to create “ServiceReference1″ under Service References in my client project, but after created Form1.cs two errors occured:

    Error 1 The type or namespace name ‘localhost’ could not be found (are you missing a using directive or an assembly reference?) D:DokukVisual Studio 2008ProjectsHostClientForm1.cs 21 13 Client

    Error 2 The type or namespace name ‘localhost’ does not exist in the namespace ‘Client’ (are you missing an assembly reference?) D:DokukVisual Studio 2008ProjectsHostClientForm1.cs 21 61 Client

  52. Balazs says:

    Ok, I found the solution:
    I had to change the line containing “localhost” to this:
    Client.ServiceReference1.HelloServiceClient proxy = new Client.ServiceReference1.HelloServiceClient();

  53. abraham says:

    Dennis,

    I referenced your arrticle in a submission to CodeProject.
    http://www.codeproject.com/KB/WCF/WCF_MSMQ_Integration.aspx

    Hope that’s ok.

  54. Dennis van der Stelt says:

    @abraham : no problem, of course!!!

  55. arun says:

    Hey how can i host service and client on different machines please provide me a url if u already hav a sample on it

  56. Dennis van der Stelt says:

    @arun: Just run the service on another machine and change the url in your web.config on your client… If that’s what you mean?

  57. Neeraj says:

    What a great example for a beginer.
    Thanks alot for this article.

  58. Barney says:

    I’m a VB.NET programmer and have managed to convert your example, albeit the following line.

    localhost.HelloServiceClient proxy = new Client.localhost.HelloServiceClient();

    Vb does’nt recognise ‘Client.localhost.HelloServiceClient()

    Can you help?

    Thanks

  59. Dennis van der Stelt says:

    @Barney : Maybe remove Client or Client.localhost altogether. If it does recognize localhost.HelloServiceClient then you’re okay, because you need exactly the same on the other end :)

  60. Sally says:

    Hi dennis…I’v successfully done a simple wcf application using your code. I needed to use a wcf service using java client though..Can you help me in this by providing the client code of your apllication in java????

  61. Dennis van der Stelt says:

    @Sally : Java? What’s that? :) But seriously, I could not tell you how to do that as I have no idea. But if you lookup how to connect to ASP.NET Webservices from Java or how to do SOAP 1.1 or SOAP 1.2 style services, than you should be okay.

    Some examples:
    http://www.codeproject.com/KB/XML/WSfromJava.aspx
    http://stackoverflow.com/questions/301113/what-is-the-best-way-to-connect-to-a-dotnet-web-service-from-java/301120
    http://forums.sun.com/thread.jspa?threadID=5369119

    and from MSDN

    http://msdn.microsoft.com/en-us/library/ms953968.aspx

    As far as I know, you do need some extra framework though. It’s not by default in java.

  62. sally says:

    thanks a lot neways…i’l check out d refernces

  63. Barney says:

    Dennis – thanks, realised this soon after posting.

    I have now tried to run the example, client on my local pc and host on a server.

    Strange – mangaged to get it working once, event change localhost.HelloService to server name after adding the service reference.

    However cannot get it working now – keep getting errors trying to add the service reference (see below)

    C:Program FilesMicrosoft Visual Studio 8Common7IDEsvcutil.exe /noLogo /s /ct:”System.ComponentModel.BindingList`1,System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089″ /d:”C:Documents and Settingss_barnacleLocal SettingsTempdhfcbxv1.uws” /config:”C:Documents and Settingss_barnacleLocal SettingsTempdhfcbxv1.uwsnewapp.config” /mergeConfig /out:”vbdev.vb” /language:vb /n:*,vbdev “http://vbdev:8080/”
    Attempting to download metadata from ‘http://vbdev:8080/’ using WS-Metadata Exchange or DISCO.
    Microsoft (R) Service Model Metadata Tool
    [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.30]
    Copyright (c) Microsoft Corporation. All rights reserved.

    Error: Cannot obtain Metadata from http://vbdev:8080/

    If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.

    WS-Metadata Exchange Error
    URI: http://vbdev:8080/

    Metadata contains a reference that cannot be resolved: ‘http://vbdev:8080/’.

    There was no endpoint listening at http://vbdev:8080/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.

    The remote server returned an error: (404) Not Found.

    HTTP GET Error
    URI: http://vbdev:8080/

    The document at the url http://vbdev:8080/ was not recognized as a known document type.
    The error message from each known type may help you fix the problem:

    – Report from ‘DISCO Document’ is ‘There was an error downloading ‘http://localhost:8080/?disco’.’.
    – Unable to connect to the remote server
    – No connection could be made because the target machine actively refused it 127.0.0.1:8080
    – Report from ‘WSDL Document’ is ‘The document format is not recognized (the content type is ‘text/html; charset=UTF-8′).’.
    – Report from ‘XML Schema’ is ‘The document format is not recognized (the content type is ‘text/html; charset=UTF-8′).’.
    – Report from ‘http://vbdev:8080/’ is ‘The document format is not recognized (the content type is ‘text/html; charset=UTF-8′).’.

    If you would like more help, type “svcutil /?”

    When i type th url into my IE on my client, it is discovererd and lists all expected code and information.

    Any ideas or thoughts warmley welcomes.

    PS i’ve tried adding all user rights to c:windowstemp folder but this has had no effect.

    Thanks
    Barney

  64. Barney says:

    Thanks Dennis – did you mangage to get this example to work across two different machines?

    So far i’ve been unable

  65. Dennis van der Stelt says:

    @Barney : Sorry mate, but your last comment can’t be approved by the system. Maybe it’s because it suspects HTML in the comment. I’ll have to look into it.

    But… Is your service actually running? If the service isn’t running (ie. the host project should be started and doing it’s work) than no one will ever be able to find the service.

    About the last comment, yes I have! :)
    First try to get it working with basicHttp and if that’s working, try other bindings. A lot of bindings can give problems with security. For example netTcp is secure by default, but if you don’t know that and/or the service is in another domain or in a workgroup, you can run into all kinds of problems.

    If you want to use authentication, try to use Windows authentication as it’s the easiest. Almost everything else needs X509 certificates.

  66. Didier M says:

    Your simple example was very helpfull. Thanks for making my day.

    Didier M

  67. Marius says:

    Hi,
    Installed all Net.3.5 SP1 VC 8, wcf framework,
    Still getting
    —— Build started: Project: wcf_node, Configuration: Debug Any CPU ——
    C:WINDOWSMicrosoft.NETFrameworkv3.5Csc.exe /noconfig /nowarn:1701,1702 /errorreport:prompt /warn:4 /define:DEBUG;TRACE /reference:”C:Program FilesMicrosoft.NETSDKCompactFrameworkv3.5DebuggerBCLSystem.Core.dll” /reference:”C:Program FilesMicrosoft.NETSDKCompactFrameworkv3.5DebuggerBCLSystem.Data.DataSetExtensions.dll” /reference:”C:Program FilesMicrosoft.NETSDKCompactFrameworkv3.5DebuggerBCLSystem.Data.dll” /reference:”C:Program FilesMicrosoft.NETSDKCompactFrameworkv3.5DebuggerBCLSystem.dll” /reference:”C:Program FilesMicrosoft.NETSDKCompactFrameworkv3.5DebuggerBCLSystem.ServiceModel.dll” /reference:”C:Program FilesMicrosoft.NETSDKCompactFrameworkv3.5DebuggerBCLSystem.Xml.dll” /reference:”C:Program FilesMicrosoft.NETSDKCompactFrameworkv3.5DebuggerBCLSystem.Xml.Linq.dll” /debug+ /debug:full /filealign:512 /optimize- /out:objDebugwcf_node.exe /target:exe Program.cs PropertiesAssemblyInfo.cs
    D:_FLwcf_nodewcf_nodeProgram.cs(11,6): error CS0246: The type or namespace name ‘OperationContract’ could not be found (are you missing a using directive or an assembly reference?)
    D:_FLwcf_nodewcf_nodeProgram.cs(11,6): error CS0246: The type or namespace name ‘OperationContractAttribute’ could not be found (are you missing a using directive or an assembly reference?)
    D:_FLwcf_nodewcf_nodeProgram.cs(8,2): error CS0246: The type or namespace name ‘ServiceContract’ could not be found (are you missing a using directive or an assembly reference?)
    D:_FLwcf_nodewcf_nodeProgram.cs(8,2): error CS0246: The type or namespace name ‘ServiceContractAttribute’ could not be found (are you missing a using directive or an assembly reference?)

  68. Dennis van der Stelt says:

    @Marius : Did you use “Using System.ServiceModel” at the top of your code as I point out in bullet 4?

    When your cursor is on the OperationContract or ServiceContract attribute, a small red line should appear, if you didn’t include the “Using System.ServiceModel”. The text should also be black if you didn’t. You can then press CTRL+. (So press CTRL and the . at the same time) and it should popup a small menu that allows you to add the using statement.

    If you _did_ use the using statement, both attributes should display in a bluish or greenish color, just like the code snippet 5 displays.

  69. Barney says:

    Dennis

    Thanks – got it working over different servers – forgot to amend the app.config to new server name.

    Brilliant example by the way

    Thanks

  70. Goran says:

    Hello Dennis,

    Your sample is excellent and I’m wondering if it’s possible to use it in Silverlight application? I’ve tried that, but there’s HelloWorldAsync method that accepts string argument, but returns void. Do you happen to know how to overcome this?

    Thank you in advance.

    Goran

  71. Dennis van der Stelt says:

    Hi Goran,

    Yes I do. In Silverlight they don’t allow you to wait for the result, because that would mean the screen would lock up. Therefor everything is asynchronous.

    There should be an event, called HelloWorldCompleted. You recognize it in the intellisense list by the lightning icon. Select it, type += behind it and press [TAB] a few times.

    A new method should be created in which you can handle the return value.

    It should look something like:

    public void MyButtonClick()
    {
    HelloServiceClient svc = new HelloServiceClient();
    svc.HelloWorldCompleted += new EventHandler(svc_HelloWorldCompleted);

    svc.HelloWorld(“dennis”);
    }

    public void svc_HelloWorldCompleted(object sender, HelloWorldCompletedEventArgs e)
    {
    string result = e.Result;
    }

    But that’s from the top of my head, so I could be wrong somewhere.

    If it doesn’t work, let me know.

  72. karthikeyan says:

    Its very good example.

  73. Stephen says:

    Hi Dennis I like your example! thanks.

  74. Cool says:

    HI,

    nice example, could u give an example code of WCF web service returning data from SQL Server.

    thanks,

  75. marek says:

    hallo,

    i am using from today VS 2008. so i want to try tu use WCF on it. but i couldn’t the Service references by the Way above. does anybody have any experience about it?

    thanks

  76. marek says:

    ich mean, i couldn’t add/create the Service references by the Way above

  77. Dennis van der Stelt says:

    You mean you’ve got this problem? What’s your actual problem?

    http://bloggingabout.net/blogs/dennis/archive/2008/03/28/quot-add-service-reference-quot-is-disabled.aspx

  78. kapil says:

    Good Article

    Thanks!
    kapil

  79. kapil says:

    Dennis van der Stelt, Your efforts are appriciated.

    I am new to WCF. based on your article, I am able to create and run WCF ….. cool

    Thanks
    kapil

  80. Its really useful for beginners in WCF.

  81. fabio says:

    hi,
    at Host.Open() i get following exception

    Message=”HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
    i visited microsoft link, but is not clear what command i have to key and if i need to open command windows from netsh tool. I’m working with win7.

    can you help me?

  82. Andrew says:

    Thank you very much!

    This example was exactly what I needed :)

    Andrew

  83. DungDLT says:

    Thanks so much!
    This sample as a start point!

  84. bas says:

    Hi,

    Please tell me how to create client in VS 2005

  85. Dennis van der Stelt says:

    @bas : Maybe this article will help : http://bloggingabout.net/blogs/dennis/archive/2006/11/09/WCF-Part-4-_3A00_-Make-your-service-visible-through-metadata.aspx

    It says something about “svcutil” and how to run it. I’m not sure if, after install of .NET 3.0 SDK, the “Add Service Reference” is available in VS2005. If not, use svcutil.exe, that’s what VS2008 uses itself.

  86. dinesh dangi says:

    hi,
    i am having a problem while running this application,as u are saying right click on host->debug->new instance then click on client -> service refrence ,its not allowing to add service reference ,its showing in disabled form.without doing debug-> new instance ,if I do the same ,then its showing error occured,as its not taking http://local host/8080/ ,saying no service is there.please look into the issue and let me know if you can fix it.

  87. Sumeet says:

    Thank you, for writing such a simple example.
    It was of great help in understanding the basic concept of WCF.

  88. Ravi Naik says:

    Good 1…………

  89. If you’re in uncomfortable position and have no cash to go out from that, you would require to receive the mortgage loans. Because that will aid you emphatically. I take collateral loan every single year and feel myself good just because of that.

  90. Ramana says:

    Can u tell how to execute this sample.breif theory how it will work.i am new to wcf.if i will keep host as startup project i am getting some window while clicking on anything it is disappearing but when i keep Client as star up page while clicking on button getting TCP error.can give some solution to this.thax in advance…

  91. surya says:

    Hi
    Can anybody plz help me out i am getting this error …

    The HttpsGetEnabled property of ServiceMetadataBehavior is set to true and the HttpsGetUrl property is a relative address, but there is no https base address. Either supply an https base address or set HttpsGetUrl to an absolute address.

  92. Dennis van der Stelt says:

    @Surya : Sure, perhaps reading this article will help.
    http://bloggingabout.net/blogs/dennis/archive/2006/11/29/WCF-Part-6-_3A00_-Address.aspx

  93. guyjasper says:

    hi dennis, i’m having problem after adding a Service Reference. just like some people above, after adding Service Reference, there is no localhost.map / localhost.cs files under Service References. I’m using VS 2008. Host was running fine.

  94. Dennis van der Stelt says:

    “localhost” is the namespace used. When you enter HelloServiceClient, Visual Studio 2008 should be able to help you find the namespace and add the using statements at the top of your code. Be careful though to enter the HelloServiceClient case sensitive, or else VS2008/VS2010 can’t help you find the namespace.

    Place your cursor in the HelloServiceClient and press CTRL + . or hover your mouse over it and click the widget that opens up.

  95. guyjasper says:

    hi dennis, i was able to make it work. in my project, it created ServiceReference1 (it was the default namespace i used when adding the Servive Reference). HelloServiceClient was an object under ServiceReference1 (as what is displayed in the object browser). i just had to replace “localhost” with “ServiceReference1″ and it works. thanks for the tutorial ;)

  96. Dennis van der Stelt says:

    @guyjasper : Glad it worked! Congrats!

  97. Krischu says:

    Does there exist a newer example for VS2008? I’m getting an error 415 when trying to add a server method and generate the client proxy file.
    C:UsersKrischuProjectsWCFWCFSimpleExample>”C:Program FilesMicrosoft
    SDKsWindowsv7.0Binsvcutil.exe” http://localhost:8008/HelloService /out:Proxy
    .cs
    Microsoft (R) Service Model Metadata Tool
    [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.2152]
    Copyright (c) Microsoft Corporation. All rights reserved.

    Attempting to download metadata from ‘http://localhost:8008/HelloService’ using
    WS-Metadata Exchange or DISCO.
    Microsoft (R) Service Model Metadata Tool
    [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.2152]
    Copyright (c) Microsoft Corporation. All rights reserved.

    Error: Cannot obtain Metadata from http://localhost:8008/HelloService

    If this is a Windows (R) Communication Foundation service to which you have acce
    ss, please check that you have enabled metadata publishing at the specified addr
    ess. For help enabling metadata publishing, please refer to the MSDN documentat
    ion at http://go.microsoft.com/fwlink/?LinkId=65455.

    WS-Metadata Exchange Error
    URI: http://localhost:8008/HelloService

    Metadaten enthalten einen Verweis, der nicht aufgelöst werden kann: “http://
    localhost:8008/HelloService”.

    Der Inhaltstyp “application/soap+xml; charset=utf-8″ wurde von Dienst “http:
    //localhost:8008/HelloService” nicht unterstützt. Möglicherweise besteht keine Ü
    bereinstimmung zwischen Client- und Dienstbindung.

    Der Remoteserver hat einen Fehler zurückgegeben: (415) Cannot process the me
    ssage because the content type ‘application/soap+xml; charset=utf-8′ was not the
    expected type ‘text/xml; charset=utf-8′..

    HTTP GET Error
    URI: http://localhost:8008/HelloService

    Fehler beim Downloaden von ‘http://localhost:8008/HelloService’.

    Fehler bei der Anforderung mit HTTP-Status 400: Bad Request.

    If you would like more help, type “svcutil /?”

    C:UsersKrischuProjectsWCFWCFSimpleExample>pause
    Drücken Sie eine beliebige Taste . . .

  98. Dennis van der Stelt says:

    @Krischu : The log says port 8008, but isn’t it 8080 in your case, just like in mine?

  99. Krischu says:

    Hi Dennis, thanks for the quick answer. I changed all (hopefully) occurences from 8080 to 8008 (since I have running other services – tomcat – on 8080).


    Christoph

  100. Dennis van der Stelt says:

    @Krischu : Let me know if it works, or doesn’t work. I’m working on an updated version of the example, including more info on how to host it inside a console app and IIS and get more bindings/protocols working.

  101. Krischu says:

    Here again the output. Must have been some problem with cut/pasting the message: (but, as said, I changed 8080 to 8008 on purpose)

    C:UsersKrischuProjectsWCFWCFSimpleExample>”C:Program FilesMicrosoft
    SDKsWindowsv7.0Binsvcutil.exe” http://localhost:8008/HelloService /out:Proxy
    .cs
    Microsoft (R) Service Model Metadata Tool
    [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.2152]
    Copyright (c) Microsoft Corporation. All rights reserved.

    Attempting to download metadata from ‘http://localhost:8008/HelloService’ using
    WS-Metadata Exchange or DISCO.
    Microsoft (R) Service Model Metadata Tool
    [Microsoft (R) Windows (R) Communication Foundation, Version 3.0.4506.2152]
    Copyright (c) Microsoft Corporation. All rights reserved.

    Error: Cannot obtain Metadata from http://localhost:8008/HelloService

    If this is a Windows (R) Communication Foundation service to which you have acce
    ss, please check that you have enabled metadata publishing at the specified addr
    ess. For help enabling metadata publishing, please refer to the MSDN documentat
    ion at http://go.microsoft.com/fwlink/?LinkId=65455.

    WS-Metadata Exchange Error
    URI: http://localhost:8008/HelloService

    Metadaten enthalten einen Verweis, der nicht aufgelöst werden kann: “http://
    localhost:8008/HelloService”.

    Der Inhaltstyp “application/soap+xml; charset=utf-8″ wurde von Dienst “http:
    //localhost:8008/HelloService” nicht unterstützt. Möglicherweise besteht keine Ü
    bereinstimmung zwischen Client- und Dienstbindung.

    Der Remoteserver hat einen Fehler zurückgegeben: (415) Cannot process the me
    ssage because the content type ‘application/soap+xml; charset=utf-8′ was not the
    expected type ‘text/xml; charset=utf-8′..

    HTTP GET Error
    URI: http://localhost:8008/HelloService

    Fehler beim Downloaden von ‘http://localhost:8008/HelloService’.

    Fehler bei der Anforderung mit HTTP-Status 400: Bad Request.

    If you would like more help, type “svcutil /?”

    C:UsersKrischuProjectsWCFWCFSimpleExample>pause
    Drücken Sie eine beliebige Taste . . .

  102. Dannyboy says:

    Greeat article. I’m stuck on step 9. When i create a new instance of the host, I am unable to right click on the client project and add the reference. I am using VS 2005. Any thoughts?

  103. prasad says:

    nice article i am searching from a long time for like this article thank u so much

  104. Dennis van der Stelt says:

    @Krischu You should probably do http:// and specify the right port number using the svcutil. So it should be something like

    svcutil.exe http://localhost:8008/HelloService /out:Proxy.cs

  105. Krischu says:

    Dennis,

    I have no idea, why the :8008 (colon – 8008) did not make it through the cut/paste mechanism into the text area in this blog. Believe me, They are all there in the original statement. So that’s definitely not he issue. I’m passing the correct URL to the svcutil command line.


    Christoph

  106. Dennis van der Stelt says:

    @Krischu: Go to the url of your service with a browser. You might’ve made a mistake in your service that makes the (http) MEX endpoint not available. The browser will most likely tell you what’s wrong.

  107. Krischu says:

    Dennis, you can see it from your own post: The URL is converted to a link. When you hover with the mouse you can see the complete URL in the status bar. This is due the lack of posting code (pre) sections in this blog.


    Christoph

  108. Krischu says:

    Hah! I figured it out:

    svcutil http://localhost:8008/?wsdl  /out:proxy.cs
    

    (localhost : 8008 that is in the above line, in case it gets eaten by the blog formatting again :)


    Christoph

  109. Dennis van der Stelt says:

    @Krischu : Contact me via the contact form. We’ll discuss this over email further! :)

  110. Enrico says:

    Hi
    i have tried this sample with Visual Studio 2008 but don’t working. I can’t do the steps from 9 in avant … where is my mistake? Thanks in advance

  111. Enrico says:

    Hi Dennis
    thanks you for your help, i solve it!!! When i run the application appeare the next error: “HTTP could not register URL http://+:8080/. Another application has already registered this URL with HTTP.SYS.”
    I think because i have publish the Host application in localhost, so how can i un-publish? Thank again

  112. Dennis van der Stelt says:

    @Enrico
    There might be two problems. In more secure versions of Windows (Vista, 7) you can’t just register addresses on ports, because you’re not allowed. Run Visual Studio as Administrator and it works.

    The other thing is that there is already running something on that port. There can’t be two listeners on the same port. So either shutdown the first application, or change the port number in your wcf service.

  113. Enrico says:

    Hi
    now all working good. I have the next question: is necessary that the service is always active for consume it from client? In test enviroment if the service it’s not live i cant consume it from client, there si something wrong yet or it’s normal so? Thanks in advance

  114. Dennis van der Stelt says:

    @Enrico: YES! :)
    But you can also host in IIS and then IIS will bring the service to life when the first call takes place. IIS will also tear it down after 20 minutes of inactivity.

  115. Krischu says:

    Caveat: When you go to VS2008 you will still see that there are different Menu items in the area of “Add Service Reference” vs. “Add Web Reference”, depending on whether to set the project based on Net 2.0, 3.0 or 3.5.

    Christoph

  116. Enrico says:

    For Host the service in IIS i must Publish it in IIS, after i must create the proxy client with svcutil tool?

  117. Dennis van der Stelt says:

    @Enrico: Either that or with “Add Service Reference” in Visual Studio :)

  118. Sree says:

    Hey,

    Thank you so much for your wonderful article.Cheers.

  119. Freshdls says:

    Your sample is excellent and I’m wondering if it’s possible to use it in Silverlight application? I’ve tried that, but there’s HelloWorldAsync method that accepts string argument, but returns void. Do you happen to know how to overcome this? I wish to use http://www.evildrome.com for all my downloads.

  120. paulhar333 says:

    Excellent article, keep up the good work. I read a lot of forums on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your forum.
    Thanks.

    Serve Technology

  121. Ray says:

    Great series of posts thanks so much for your time in writing them and answering questions. What a good web samaritan you are! ;.)
    Question please: I created a web service from a msdn article-basichttpbinding and I can only get to the /MEX endpoint. I can’t browse to the svc or svc?wsdl
    How do I enable this for sharepoint?
    TIA.
    Ray

  122. sandeep says:

    Hiiiii Dennis

    i am getting folling error ..please help over that

    Could not connect to http://localhost:8081/HelloService. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8081.

  123. sandy says:

    can u give example for two machine client and server connected through LAN

  124. SNS says:

    Hi Dennis,
    Awesome man, Really simple & nice blog for understanding WCF.

    Thanks
    SNS

  125. Rajendra prasad says:

    Hi Dennis,

    I have created same as you given in blog.host application executed correctly then i created Client Application same as you given.Here am getting error at local and client.am included namesapce(service refrence added in client).the following code written i client application.

    using System.ServiceModel;
    using System.ServiceModel.Description;

    localhost.HelloServiceClient proxy = new Client.localhost.HelloServiceClient();
    string result = proxy.HelloWorld(textBox1.Text);
    MessageBox.Show(result);

    please help where done mistake in my application.

    Thanks & Regards,
    Rajendra prasad.

  126. Dennis van der Stelt says:

    Rajendra,

    The mistakes are most likely in the configuration. And as you don’t provide the error message itself, I really don’t see how I can help you.

  127. Rajendra Prasad says:

    Hi Dennis,

    I have created same as you given in blog.host application executed correctly then i created Client Application same as you given.Here am getting error at local and client.am included namesapce(service refrence added in client).the following code written i client application.

    using System.ServiceModel;

    using System.ServiceModel.Description;

    localhost.HelloServiceClient proxy = new Client.localhost.HelloServiceClient();

    string result = proxy.HelloWorld(textBox1.Text);

    MessageBox.Show(result);

    while running am getting error as below listed:
    The type or namespace name ‘localhost’ could not be found (are you missing a using directive or an assembly reference?)
    The type or namespace name ‘Client’ could not be found (are you missing a using directive or an assembly reference?)

    my configuaration file code listed below:

    please help where done mistake in my application.

    Thanks & Regards,

    Rajendra prasad.

  128. Dennis van der Stelt says:

    First, your configuration is wrong. It says the endpoint its address is at “localhost/HelloService” and that’s not a uri. It’s probably “http://localhost/HelloService” or “http://localhost:8080/HelloService” if you’ve followed the tutorial correctly.

    When you created the service reference, something was different than in my example or initial setup. At the bottom of the screen you specify the namespace. As shown in this screen:
    http://bloggingabout.net/cfs-filesystemfile.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/dennis.metablogapi/4784.CreateServiceReference_5F00_34697BA6.png

    In that screenshot, it says “ConsoleClient” and in the tutorial above it suspects that “localhost” is there. That’s your problem when it says it can’t find localhost. It’s not a WCF problem, it’s a .NET namespace/type problem that’s found at compile time.

  129. Joyjit says:

    Extremely helpful example. Thanks Dennis.

  130. Shri says:

    good exmaple.
    steps to test the project:
    ==========================
    1) run host project(open only host project and run)
    2) then only you will be able to add “http://localhost:8080/” to the client project
    3)now run client project and test it.
    Note: add servicereference is nothing but addwebreference.

  131. Dennis & Ralf, thank you both for this lovely article. Please keep the articles coming that make understanding new concepts easy.

    Thanks again,
    Jessi

  132. vincent says:

    Hi Dennis, good article for startup. I was wondering what MEX endpoint for, I commented the line and I can still browse from IE and call the service in the windows app. So what’s it used for?

  133. Friend says:

    Very nice article …thanks a lot friend…keep it up

  134. Dennis van der Stelt says:

    @vincent: The MEX endpoint is for discovery of the contracts. In other words, without it you should not be able to discover what actions (commands/methods) are available and what they require as input.

    The fact that your HTTP endpoint is still visible, can have multiple reasons. For example, in .NET 4.0 the MEX endpoint is automatically added to the service, even if you don’t specify it! :)

  135. jony says:

    Could not connect to http://localhost:8080/HelloService. TCP error code 10061: No connection could be made because the target machine actively refused it 127.0.0.1:8080. help

  136. Dennis van der Stelt says:

    @Jony : It’s really hard with so little information. It’s on localhost so I presume you can connect to it. Sometimes going over machine boundaries makes problems harder. But it could be all kinds of problems. Server not running, not running correctly, binding not setup, etc, etc, etc…

  137. Ashish says:

    Thanks buddy, Its Great

  138. Steve says:

    It’s a great smaple for me to start lean WCF. Thanks!

  139. Ritesh says:

    Hey thanx a lod buddy, you know i also implemented image upload through ajax using REST webservice its GREAT!!!!. I needed a mechanism to upload images ajax based on my website http://www.jalandharsearch.com and it worked like a charm.Thanks again

  140. Huzefa says:

    Hi,
    I tried implementing the service but I am getting the ‘AddressAccessDeniedException’ Http could not register the url….
    I have read all the above comments and have tried to do the following:
    1.Run as admin. (am using vista, VS2010)
    2. tried to change the port no. from 8080 to 1337 or 8081 but still the same. I checked my port 8080 or nay of these are not used by any other application.
    3. when I try to run it as admin, I am getting the following error:
    VS cannot start debugging because the target c:…Host.exe is missing. Please build and retry.

    I would really appreciate if you could help. I am going crazy trying to grasp wcf since past one week, but fail. Finally reading the comments and article here, got some HOPE!

  141. Dennis van der Stelt says:

    @Huzefa : Try the following article as well:
    http://bloggingabout.net/blogs/dennis/archive/2010/06/16/wcf-simple-example-in-visual-studio-2010.aspx

    I really, really need to create a webcast of this and show how easy it _should_ be. Of course there can be all sorts of problems due to things not working because of security settings, for example.

    When you say, run as admin, do you mean run under the local administrator account (!) or that you select “Run as administrator” from the context menu, when you right-click on VS2010 icon?

    And can you specify what …host.exe is completely? Is it svchost.exe or something else?

  142. Huzefa says:

    Run as administrator as in from the context menu by right-click. By …host.exe , I meant the path in my C: drive where the error message is pointing that the host.exe should be present.

  143. webhostuk says:

    Nice work done, most of our customer ask for this, I will share this article in our knowledge base.

  144. Eslam says:

    hi,thanks for your illustration but i have an error which is ‘ Error 2 The type or namespace name ‘localhost’ could not be found (are you missing a using directive or an assembly reference?) E:Documents and SettingsEslam FaragMy DocumentsVisual Studio 2008ProjectsWebSiteClient_ConsumeCode.cs 23 15 Client_Consume

    so,could you mind helping me,Thanks for your kind wishes.

  145. Charlie D says:

    Here is a great article to help you when you are starting a WCF project: http://msdn.microsoft.com/en-us/library/hh27311

  146. Dennis van der Stelt says:

    @Charlie D : Page not found?! :)

  147. Shirish Bhavthankar says:

    Thanks a lot. Nice.

  148. Thank you, this is a great article. I’m looking for an example of starting a WCF SOAP Project in VB.NET, can anyone recommend a website? Thank you.

  149. Dennis van der Stelt says:

    Should be exactly the same, except for syntactic stuff that differs from VB en C#. Like defining variables and such.

  150. very good and very easy to understand
    many many thanks for the easiest example and the way of explanation is very user friendly and simple english with correct technical way

  151. Shahnawaz says:

    Hi,
    Provide details about the WCF. this article is too short.
    Publish a real life example, that will be more useful and understandable. good short of attempt to write on WCF, some how it is useful for the lay man, but i need improvement from your side, so that many views can be benefited.
    Thanking you
    Shahnawaz

  152. Dennis van der Stelt says:

    @shahnawaz: write longer comments, this one is too short.
    Seriously, try the WCF tag on this blog and find dozens of articles. Also try reading MSDN for more views. Can’t provide everything in a single post, especially not a ‘simple example’.

  153. It’s really helpful….You post “WCF Simple Example” was amazing…It encourage me for further readings. It just clear my basic doubts and Now i am able to understand advance topices on WCF. Thnx “Dennis van der Stelt”

Click on a tab to select how you'd like to leave your comment

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>