<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blinnov's blog &#187; .NET</title>
	<atom:link href="http://www.blinnov.com/category/net/en+ru/feed/en+ru/" rel="self" type="application/rss+xml" />
	<link>http://www.blinnov.com</link>
	<description>another brilliant mind poisoned by c++</description>
	<lastBuildDate>Fri, 10 Feb 2012 11:08:41 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Unmanaged C++ client for WCF service.</title>
		<link>http://www.blinnov.com/2008/01/22/wcf-service-unmanaged-client/en/</link>
		<comments>http://www.blinnov.com/2008/01/22/wcf-service-unmanaged-client/en/#comments</comments>
		<pubDate>Tue, 22 Jan 2008 04:59:23 +0000</pubDate>
		<dc:creator>vital</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[Work]]></category>

		<guid isPermaLink="false">http://www.blinnov.com/2008/01/22/wcf-service-unmanaged-client/</guid>
		<description><![CDATA[Recently I have been evaluating different options and solutions for creating distributed network applications. Of course, this definition is too broad and there might be hundreds of answers and they all would be useful in different circumstances but I paid special attention to WCF.
As Microsoft released a Visual Studio 2008, I can hardly see any [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I have been evaluating different options and solutions for creating distributed network applications. Of course, this definition is too broad and there might be hundreds of answers and they all would be useful in different circumstances but I paid special attention to WCF.</p>
<p>As Microsoft released a Visual Studio 2008, I can hardly see any reasons why someone who is up to developing a distributed network application might choose anything different. New Studio even has a project template for WCF services, what makes network servers and clients development as easy as can be&#8230; But, of course, that would only work if no other platform except Windows is considered. Which is not uncommon.</p>
<p>So, what options are there if we need to build a client for existing WCF service  that would work where .NET framework is not installed (because it is not available for that platform)? There is one way offered by Microsoft (using sproxy.exe) and <a href="http://icoder.wordpress.com/2007/07/25/consuming-a-wcf-service-with-an-unmanaged-c-client-with-credential-passing/">some could use it</a>. Other way involves writing &#8220;moniker&#8221; in .NET and then use it via COM. They both would work on windows platform only, moreover, I couldn&#8217;t make sproxy.exe to work with WCF service at all, so I had to look for the third option, which was <a href="http://www.cs.fsu.edu/~engelen/soap.html">gSOAP</a>.  And this worked.<br />
<span id="more-130"></span></p>
<p>Of course, you&#8217;ll need to download and install (and, probably, build before installing)  <a href="http://www.cs.fsu.edu/~engelen/soap.html">gSoap</a>, what, I hope, will not be a big problem. Then let&#8217;s assume we have a WCF service with following service contract running (this is just an example, I can hardly imagine a need in remote string concatenation service):</p>
<pre name="code" class="c#">
namespace SimpleServer
{
[ServiceContract]
public interface iStringService    {
[OperationContract]
/**
* &lt;summary&gt;
* String concatenation
* &lt;/summary&gt;
* */
string Concat(string first, string second);
}
}</pre>
<p>Writing, compiling and hosting the service is out of the scope of this article, however, I need to note that the only binding type I could make to work with gSoap with the service was Basic Http &#8211; keep it in mind when hosting your service.<br />
Then is a step where a controversial feature of VS 2008 I have already written about (wcfsvchost.exe) really helps us &#8211; we need to get the service hosted and running before start using gSOAP. Run the project and have service hosted.</p>
<p>gSoap comes with two tools that make all stubs and bindings to programming language, which could be either C or C++, however, I was only interested in writing application in C++, which turned to be pretty straightforward.</p>
<p>At first, you&#8217;ll need to use wsdl2h.exe and the WCF service I mentioned above hosted and running. Assume that service entry point is <em>http://localhost:8080/MyService/</em>, and service WSDL is available on URL <em>http://localhost:8080/MyService/metadata/?wsdl</em>. This means that service endpoints&#8217; configuration looks like the following:</p>
<pre name="code" class="xml">
    <services>
      <service behaviorconfiguration="SimpleServer.Service1Behavior">
        name="SimpleServer.Service1"&gt;
        <endpoint address="http://localhost:8080/SimpleServer/Service1/">
          binding="basicHttpBinding" bindingConfiguration="HttpBase" bindingNamespace=""
          contract="SimpleServer.iSimpleService"&gt;
          <identity>
            <dns value="localhost">
          </dns>
        </identity>
        <endpoint address="mex" binding="mexHttpBinding">
          contract="IMetadataExchange" /&gt;
        <host>
          <baseaddresses>
            <add baseaddress="http://localhost:8080/SimpleServer/Service1/metadata/"></add>
          </baseaddresses>
        </host>
      </endpoint>;</endpoint></service></services></pre>
<p>Note &#8211; Visual Studio can generate metadata endpoint for you with some URL like &#8220;<em>http://localhost:8978/Design_Time_Addresses&#8230;</em>&#8220;, which can be used as well.</p>
<p>Execute the following:</p>
<p><em>wsdl2h.exe -o myService.h  </em><em>http://localhost:8080/MyService/metadata/?wsdl</em></p>
<p>It will create a file myService.h contains some basic bindings enough to write SOAP client in C. As I wanted to use C++, I had to move a little bit further and take a next step:</p>
<p><em>soapcpp2 -i -I ..\gsoap\import  -x -C myService.h </em></p>
<p>Option -i tells compiler to derive SOAP proxies from soap struct. This is not necessary, but I noticed the code looks less messy this way.<br />
Option -I specifies path to import directory in gSOAP distribution.<br />
Option -C enables client-side code generation only<br />
and -x is needed if you don&#8217;t need to have any sample XML message files.</p>
<p>This will generate proxies for all service&#8217;s binding types that were configured, but we are only interested in ones that start with &#8220;soapBasicHttpBinding_&#8221; (in my case, the full file names were soapBasicHttpBinding_USCOREiStringServiceProxy.h and soapBasicHttpBinding_USCOREiStringServiceProxy.cpp)  because others do not usually work (I failed with net.tcp and did not try wsHttpbinding at all).</p>
<p>Now let&#8217;s use this proxy:</p>
<pre name="code" class="cpp">
#include  "soapBasicHttpBinding_USCOREiStringServiceProxy.h"
#include "soapBasicHttpBinding_USCOREiStringServiceProxy.h"
#include "BasicHttpBinding_USCOREiStringService.nsmap"
#include &lt;iostream&gt;

int main(int argc, _TCHAR* argv[])
{
    BasicHttpBinding_USCOREiStringServiceProxy MyProxy;
    /* Uncomment two following strings and modify path to the
    service according to your network configuration */
    // static const char* const pszEndPoint="http://localhost:8080/MyService/";
    // MyProxy.soap_endpoint=pszEndPoint;
    std::string s1("First string");
    std::string s2("Second string");
    // Note this:
    _ns1__Concat Param;
    _ns1__ConcatResponse Response;
    Param.first = &amp;s1;
    Param.second = &amp;s2;
    if (MyProxy.Concat(&amp;Param,&amp;Response)==SOAP_OK)
    {
     std::cout
           &lt;&lt; "Service responded: "
           &lt;&lt; (*Response.ConcatResult)
     ;
    }
    else
    {
    std::cout
         &lt;&lt; "SOAP service call error"
         ;
    }
}</pre>
<p>This still looks a little messy but gives an idea. If you look at BasicHttpBinding_USCOREiStringServiceProxy class implementation, you will see that each method of WCF service has corresponding method in that class, but signature is different. Method Concat has two parameters, first for input values and second for output, what explains what generated  _ns1__Concat and _ns1__ConcatResponse structures are used for.</p>
<p>That&#8217;s it &#8211; this little example worked. Of course, you&#8217;ll need to add generated files into your project as well as stdsoap2.cpp (which can be found in main directory of gSOAP) and pay attention to *.nsmap file.</p>
<p>Of course, this does not cover all possible questions that may arise, but it gives a good starting point. From my point of view, gSOAP would be a good solution when there is a need to write a client for <em>existing</em> WCF service.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blinnov.com/2008/01/22/wcf-service-unmanaged-client/en/feed/en/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
		<item>
		<title>WCF services development made easy in VS 2008?</title>
		<link>http://www.blinnov.com/2007/12/19/wcf-services-development-made-easy-in-vs-2008/en/</link>
		<comments>http://www.blinnov.com/2007/12/19/wcf-services-development-made-easy-in-vs-2008/en/#comments</comments>
		<pubDate>Wed, 19 Dec 2007 03:50:58 +0000</pubDate>
		<dc:creator>vital</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Work]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.blinnov.com/2007/12/19/wcf-services-development-made-easy-in-vs-2008/</guid>
		<description><![CDATA[This is about experience dealing with WCF templates in newly released VS 2008. Had a lot of fun with WcfSvcHost.exe]]></description>
			<content:encoded><![CDATA[<p>Or what?</p>
<p>Yes, I am C++ maniac and would write everything in C++, if I could, but I am not an idiot. I have written distributed network applications in C++ before, this is why for a new project I decided to see how .Net eases such tasks.</p>
<p>That&#8217;s true &#8211; <a href="http://msdn2.microsoft.com/en-us/library/ms735119.aspx">Windows communication foundation</a> looks pretty good. It&#8217;s straightforward, it hides all network stuff behind and even newly released VS 2008 has sufficient project templates that make WCF services creation as easy as can be.</p>
<p><span id="more-94"></span></p>
<div class="code">As it turned, not quite. If you really want to create distributed network system where different systems and programming languages may be involved, better consider ICE (<a href="http://www.zeroc.com">www.zeroc.com</a>), for example. At the moment I am busy testing and comparing performance of all such libraries I can find and ICE is at least as twice as faster than WCF using TCP binding. And you know what? Writing ICE server and client parts is even easier than dealing with WCF, not mentioning that I could write server in C++ and .NET client for it in half an hour. The only issue is that there is no wizards for ICE built in VS.</div>
<p>Only a few clicks and you&#8217;ve got a fully functional skeleton for the service &#8211; just add some business logic and you are ready to go. Even more, new studio helps you debug these services &#8211; as you try to run it (remember trying to run a DLL project in Studio 2003 and scary error messages?) Studio kindly starts a process that hosts your service (WcfSvcHost.exe) and shows a nice client that allegedly helps you debugging that service. Isn&#8217;t it what we all were dreaming about? Finally, distributed application development became really easy!</p>
<p>Well, it wouldn&#8217;t be a Microsoft creation if there were no hidden surprises. In this case, great feature of Studio that allows developer to start a newly created WCF service without a hassle of writing hosting application by her- or himself may become a nightmare.</p>
<p>Studio tends to host and start  all WCF services from current solution even if you want run and debug completely different project which may not use any of those services at all! It just does it because these projects are there. And it gets worse when the application consists of a few WCF service projects and Windows or console or NT service project that, as developer intended, should host these services in production. Just imagine &#8211; you want to see if your program starts and starts these services, but before you see it running, you&#8217;ll watch notification that &#8220;Your service(s) have been hosted&#8221;. Then you realize that all your services are hosted somewhere else but not by where you wanted them to be. If you did not modify service entry point in App.config,  you&#8217;ll see exceptions coming through as &#8220;another application has already registered that address&#8221;. Of course, it did. WcfSvcHost.exe did it for us.</p>
<p>I tried to find the way of switching it off but all my efforts have failed. I <a href="http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2486936&amp;SiteID=1">found a post</a> with similar problem and the only answer was</p>
<blockquote><p>If the WCF projects are in the same solution as your WPF app, the debugger will also startup your services. This makes the debugging experience simpler where you can simply set a breakpoint in the WCF service and break whenver the WPF app calls into the WCF service.</p></blockquote>
<p>Isn&#8217;t it pretty? It makes debugging simpler! But who says I was going to debug anything?</p>
<p>I wouldn&#8217;t be debugging anything at all. I forgot how to use debugger &#8211; for a last few years I completed several projects (in C++, by the way) and they did not have any memory leaks, crashes, performance problems and other nasty things. Without using debugger at all.  I just prefer unit tests.</p>
<p>That&#8217;s all &#8211; great idea, nice project template, but a little feature that breaks everything apart.</p>
<p>Or I just missed a check box somewhere?</p>
<p>No, I did not. All my efforts to stop VS from starting WcfSvcHost.exe each time I run any project were fruitless. But I could stop it from hosting services by removing  (or making invalid) app.config in corresponding projects. At least it prevents hosting process from starting the service and occupying a port which I was going to use.</p>
<p>Upd: Just found <a href="http://www.pluralsight.com/blogs/aaron/archive/2007/11/15/49165.aspx">similar question</a> and possible workaround:</p>
<blockquote><p>you can remove the ProjectTypeGuids tag from the project file as a workaround.</p></blockquote>
<p>Another update: After some research I found a way to <a href="http://www.blinnov.com/2008/01/22/wcf-service-unmanaged-client/en/">consume WCF service with unmanaged C++ client</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.blinnov.com/2007/12/19/wcf-services-development-made-easy-in-vs-2008/en/feed/en/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

