Geeks With Blogs

News Bob Taco Industries is an ISV focused on game and app development for Microsoft platforms headed up by Michael B. McLaughlin. Mike is a Microsoft Visual C++ MVP (previously an XNA/DirectX MVP from 2011-2013), a developer, a writer, a consultant, and a retired lawyer. If you're a developer who is just getting started, consider checking out the BTI website's section for developers for links to code samples and other helpful sites.
Bob Taco Industries Blogging Division

Update 1 (Sept. 17, 2010):

While the part below about using SLSvcUtil.exe to generate the service reference undoubtedly still works, it is no longer necessary. The RTM version of the Windows Phone Developer Tools now properly supports right-clicking on your game project's "References" item in the Solution Explorer and adding a Service Reference that way. ChannelFactory and related methods remain unsupported. For more details see (Networking in Silverlight for Windows Phone), which appears to have been updated since I first wrote this post.


There’s been much ado about how to get a WCF service working in XNA on WP7. Most solutions involve some form of creating a Silverlight something or other and slinging WCF in from there. Those days are over. Here’s what you need: the Windows Phone Developer Tools and Visual Web Developer 2010 Express (both are free; I’ve also tested this with Visual Studio 2010 Ultimate which, not surprisingly, handled it just fine :) ).

First, let’s create a web service. Open up Visual Web Developer 2010 Express. Go to “File->New Project”. It may have you in Visual Basic. This would probably work fine, but I work much more in C# so I switched to Visual C# under Installed Templates and then chose “WCF Service Application”. I set a name (I chose WcfXnaTestService) and hit “OK”. Like magic, my web service sprang forth. The default service doesn’t do much – it takes in an int and spits back a string – but who cares! Go to the “Build” menu and choose “Build WcfXnaTestService” (or whatever you named your service). Then go to “Debug->Start Debugging”. This should start up a local webserver and open up a copy of either Internet Explorer or something called WCF Test Client.

If you get IE, click on the file that says “Service1.svc”. This will open a webpage telling you about your service. At the top will be a line that looks something like: “svcutil.exe http://localhost:64152/Service1.svc?wsdl”. Right click on the link, and choose “Copy Shortcut”.

If you get WCF Test Client, on the left you will see “My Service Projects”. If it has a little + next to it, click it to expand it. You will then see something that looks something like “http://localhost:64152/Service1.svc”. (Note: 64152 is just the port it happened to assign me on this run, you could get any high number). Right click on the localhost address and choose “Copy Address”.

Now don’t close any of that, whichever one you got. Just leave it be. We want that server to keep running until we’re completely and totally done with this entire tutorial. So minimize it or whatever, but leave it all running (including Visual Web Developer too, of course).

At this point you need to open a command prompt. Yep, land of dragons and wizards. Press the Windows Key + R to open up a Run dialog box (this works in both Windows 7 and Windows Vista; alternately, go to the Start menu and choose Run). Type “cmd” and hit enter. This will bring up a command prompt with you in your user directory. Assuming you have the tools installed in the default location, if you have a 32-bit version of Windows installed, you will run the following command:

C:\Program Files\Microsoft SDKs\Windows Phone\v7.0\Tools\SlSvcUtil.exe http://localhost:64152/Service1.svc?wsdl

if you have 64-bit Windows, you will instead run:

C:\Program Files (x86)\Microsoft SDKs\Windows Phone\v7.0\Tools\SlSvcUtil.exe http://localhost:64152/Service1.svc?wsdl

You’ll notice that we’re using that address we copied earlier. You can simply right click in the command prompt window and choose “Paste” to insert the correct one for you (if you copied the command from this tutorial, you just need to change the port (that 64152 number) to whichever one your computer decided to open for you). Note that if you do paste from earlier and you got WCF Test Client, you will need to add “?wsdl” to the end of the url just as it appears above.

Hit enter. It’ll tell you something like “Attempting to download metadata from ‘http://localhost…. etc. etc. DISCO (seriously) … Generating files… (file names).

It should give you Service1.cs and ServiceReferences.ClientConfig assuming you didn’t change anything.

Now, open up Visual Studio 2010 Express for Windows Phone and make a new XNA game. I’m going to assume you know how to do this. Once you have your XNA game, add those files it generated to your game project (you can drag and drop or add them by right clicking on the game project in the Solution Explorer and choosing “Add->Existing Item…” or however you want). Note: not the content project, the game project. Once they are all in, you must do two things. First, right click on ServiceReferences.ClientConfig and set choose “Properties”. Set the “Build Action” to “Content” and set “Copy to Output Directory” to “Copy if newer”. I’m not sure that you have to set it to “Content” but you must set it to copy into the output directory to make sure it’s included in the XAP file. Next, switch back to the Solution Explorer and right click on “References” in your game project. You will need to add references (if you don’t already have them) to System.Net, System.Runtime.Serialization, and System.Servicemodel. Try compiling your game. It should compile. If not, add more references (make educated guesses based on the error messages – but those were all I had to add). Then you can add an instance of the WCF service to your game. I used the stock game that comes with the Windows Phone Developer Tools just to keep things simple. I added a SpriteFont to my content project, added the following field to my Game1 class:

        SpriteFont spriteFont;
        string result = "";
        Service1Client service1Client;
        bool hasSentRequest;


then in LoadContent I loaded my sprite font and instantiated my Service1Client using the parameter-less constructor:

service1Client = new Service1Client();


I then typed in,

service1Client.GetDataCompleted += 


at which point auto completion shows up and says to hit TAB to add in the rest and then to hit TAB again to generate the callback method. Two tabs produced:

service1Client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(service1Client_GetDataCompleted);


with a new method:

void service1Client_GetDataCompleted(object sender, GetDataCompletedEventArgs e)
    // It started with the below line, which I eliminated and replaced with the rest of what appears
    // throw new NotImplementedException();
    result = e.Result;


I added lines in Draw to draw the string called result and then in update I added:

            if (gameTime.TotalGameTime.TotalSeconds > 15 && !hasSentRequest)
                hasSentRequest = true;


This waits for 15 seconds then starts the process of getting data from the WCF service (which is still running, right? You didn’t close it accidentally?). This will return quite quickly, setting what was our empty result string to “You entered: 30” which will appear right on screen courtesy of SpriteBatch which has been drawing the empty string this whole time and now draws the newly populated one.

So there you have it. I won’t really go into detail about all the ways I tried that didn’t work. I finally got to a point where I was trying to implement a ChannelFactory(TChannel) which gave me a NotSupportedException when I did implement it by hand (because it wasn’t implementing itself) and that triggered a memory that led me to MSDN and this page: (“Networking for Windows Phone”, specifically the “Implementation Differences” heading) which in turn led me to this page: (“Using SLsvcUtil.exe to Access a Service”). Once I found that, it was done. A quick search on my computer found the location of SLsvcUtil.exe (which you saw earlier in this post). A quick run showed me it was a command line utility. And then a quick foray into the command prompt showed me how it worked.

One final note: unless and until you are hosting this on a “real” website (or at least on a fixed port on your computer rather than one that will most likely change each time you close Visual Web Developer and then restart it later), you’ll need to remember to open up ServiceReferences.ClientConfig and change the port number to whatever port you happened to get on the new run. It’s in the address in the “endpoint” tag – it’s a small file so you’ll see it right away. Sorry it’s been a while. I’ve been working on something “big” that I hope to have done and ready to go soon. Until next time, good luck!

Posted on Monday, August 30, 2010 7:36 PM xna , wp7 , tutorial | Back to top

Comments on this post: WCF and XNA on WP7 – Hack Free

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...

I have 2 errors yet

Custom tool error: Failed to generate file: The system cannot find the file specified. (Exception from HRESULT: 0x80070002)

The type or namespace name 'ChannelBase' could not be found (are you missing a using directive or an assembly reference?)

help , plz
SRY about bad english.
Left by Farhad on Sep 06, 2010 6:58 AM

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...
My first error solved , but second not

I do this work , but I have 1 error yet , help me plz

in myservice.cs
ChannelBase<...> in line 442 have error
Left by Farhad on Sep 06, 2010 7:48 AM

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...
The remarks for ClientBase<TChannel> ( - the class in which ChannelBase<T> is found ( is, according to its remarks, simply a wrapper around the ChannelFactory<TChannel> class. ChannelFactory is not supported in Windows Phone unfortunately ( You'll need to rework your code so that it doesn't make use of anything related to ChannelFactory. Hope this helps. Good luck!
Left by MikeBMcL on Sep 06, 2010 10:42 AM

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...
I resolve that
no error now
but I have new problem !

I added 2 files to my project:
And I done all work

but in code (Game1.cs) I cant use that

PlayerClient Client = new PlayerClient("BasicHttpBinding_IPlayer");

this code have problem , PlayerClient is unknown , Like the serviceReference not added , help me plz

Sry about bad english .
Left by Farhad on Sep 07, 2010 3:53 PM

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...
Can I use consoleapplication in my project and add wcf reference to it and add it to my xna game , and use from that reference ?
If I can , how ?
Left by Farhad on Sep 07, 2010 4:48 PM

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...
Re: PlayerClient - did you make sure to add any necessary using statements? If your classes are in a different namespace you'll need to add a using ________; at the top of the .cs file you want to use the class in. Also, I noticed that the .cs file is called PlayerService.cs but you are referencing PlayerClient class - are you sure you're using the right class name?

As far as a Console app, I really don't know and don't have time to test it right now, unfortunately. Try playing around with it some and see if you have any luck. I don't think that a console application is going to mesh well if it's a WP7 game though, since there's no "console" on the WP7 unlike on Windows. Good luck!
Left by MikeBMcL on Sep 07, 2010 5:38 PM

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...
ty for your time

I used SvcUtil.exe not SlSvcUtil.exe
And its worked

sorry dude I taked ur time .
Left by Farhad on Sep 07, 2010 6:05 PM

# re: WCF and XNA on WP7 – Hack Free
Requesting Gravatar...
Not a problem. Glad you were able to find the problem and solve it! There can also be two SlSvcUtil.exe programs if you have the Silverlight 4 tools installed - I haven't tried it, but I expect that the SL4 wouldn't output working files either.

There's a good chance that right clicking to add a service reference right in Visual Studio will be working when the dev tools go RTM on the 16th - I know there's an XNA CC forum post in which one of the XNA dev team members mentioned that they are working on it and hope to get it fixed in a future release (which is as much confirmation as marketing lets them give on anything). I'm hopeful it'll be done in RTM since command line stuff is easy to make mistakes with, especially when you're digging many layers deep to find an executable as in this case. Anyway, glad it's working!
Left by MikeBMcL on Sep 07, 2010 9:58 PM

Comments have been closed on this topic.
Copyright © Michael B. McLaughlin | Powered by: