<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Writing custom requests to simple WCF services</title><link>http://blogs.msdn.com/carlosfigueira/archive/2008/01/13/writing-custom-requests-to-simple-wcf-services.aspx</link><description>Quite often one needs to talk to a WCF service, but using a (WCF) proxy is not a viable alternative. Sometimes the language used isn't a .NET one, the client might not have the .NET framework installed, or the overhead of the proxy is too big for the</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>re: Writing custom requests to simple WCF services</title><link>http://blogs.msdn.com/carlosfigueira/archive/2008/01/13/writing-custom-requests-to-simple-wcf-services.aspx#9427065</link><pubDate>Tue, 17 Feb 2009 08:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9427065</guid><dc:creator>burhaan</dc:creator><description>&lt;p&gt;Great post carlos, Just a little thing that i needed to ask.&lt;/p&gt;
&lt;p&gt;I have gotten myself into a situation that requires me to use a socket based client to communicate with a WCF service that is hosted on net.tcp protocol.&lt;/p&gt;
&lt;p&gt;How can I manage to make this communication possible?&lt;/p&gt;
&lt;p&gt;I have tried to use TcpClient to send a simple text as request but I got the exception: &amp;quot;The existing connection was forcibly closed by remote host&amp;quot;.&lt;/p&gt;
&lt;p&gt;Then i tried to use a WCF only client and it succeeded (as expected). So, I got into IDispatchMessageInspector and managed to trace the message that the WCF client sent.&lt;/p&gt;
&lt;p&gt;After getting the message, I sent the exact same message via socket and the dreadful exception came back to haunt me.&lt;/p&gt;
&lt;p&gt;Can you suggest some solution for this case? Any hint, any existing article?&lt;/p&gt;
&lt;p&gt;Thanks again for the great post.&lt;/p&gt;
</description></item><item><title>re: Writing custom requests to simple WCF services</title><link>http://blogs.msdn.com/carlosfigueira/archive/2008/01/13/writing-custom-requests-to-simple-wcf-services.aspx#9442156</link><pubDate>Tue, 24 Feb 2009 07:26:19 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9442156</guid><dc:creator>carlosfigueira</dc:creator><description>&lt;p&gt;One thing you can try is to use first a HttpWebRequest (a little lower level than WCF client, a little higher than sockets) to get that working, then move down the level as that goes well. The example above, using a custom HTTP request, is shown below (I just tried building and running it, it worked :)&lt;/p&gt;
&lt;p&gt;using System;&lt;/p&gt;
&lt;p&gt;using System.IO;&lt;/p&gt;
&lt;p&gt;using System.Net;&lt;/p&gt;
&lt;p&gt;using System.Runtime.Serialization;&lt;/p&gt;
&lt;p&gt;using System.ServiceModel;&lt;/p&gt;
&lt;p&gt;using System.ServiceModel.Channels;&lt;/p&gt;
&lt;p&gt;using System.Text;&lt;/p&gt;
&lt;p&gt;[DataContract(Namespace=&amp;quot;&lt;a rel="nofollow" target="_new" href="http://my.data.contract.namespace&amp;quot;"&gt;http://my.data.contract.namespace&amp;quot;&lt;/a&gt;)]&lt;/p&gt;
&lt;p&gt;public class MyDC&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[DataMember]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public string str = &amp;quot;The string&amp;quot;;&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;[ServiceContract]&lt;/p&gt;
&lt;p&gt;public interface ITest&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[OperationContract]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;string EchoString(string text);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[OperationContract]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;int Add(int x, int y);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;[OperationContract]&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;MyDC EchoDC(MyDC input);&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class Service : ITest&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public string EchoString(string text)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return text;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public int Add(int x, int y)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return x + y;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public MyDC EchoDC(MyDC input)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return input;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
&lt;p&gt;public class A&lt;/p&gt;
&lt;p&gt;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;static Binding GetBinding()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BasicHttpBinding result = new BasicHttpBinding();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return result;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;public static void Main()&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string baseAddress = &amp;quot;http://&amp;quot; + Environment.MachineName + &amp;quot;:8000/Service&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;host.AddServiceEndpoint(typeof(ITest), GetBinding(), &amp;quot;&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;host.Open();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;Host opened&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(baseAddress);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;req.Method = &amp;quot;POST&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;req.ContentType = &amp;quot;text/xml; charset=utf-8&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;req.Headers[&amp;quot;SOAPAction&amp;quot;] = &amp;quot;&lt;a rel="nofollow" target="_new" href="http://tempuri.org/ITest/EchoDC&amp;quot;;"&gt;http://tempuri.org/ITest/EchoDC&amp;quot;;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;string reqBody = @&amp;quot;&amp;lt;s:Envelope xmlns:s=&amp;quot;&amp;quot;&lt;a rel="nofollow" target="_new" href="http://schemas.xmlsoap.org/soap/envelope/&amp;quot;&amp;quot;&amp;gt;"&gt;http://schemas.xmlsoap.org/soap/envelope/&amp;quot;&amp;quot;&amp;gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;&amp;lt;s:Body&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;EchoDC xmlns=&amp;quot;&amp;quot;&lt;a rel="nofollow" target="_new" href="http://tempuri.org/&amp;quot;&amp;quot;&amp;gt;"&gt;http://tempuri.org/&amp;quot;&amp;quot;&amp;gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;input xmlns:a=&amp;quot;&amp;quot;&lt;a rel="nofollow" target="_new" href="http://my.data.contract.namespace&amp;quot;"&gt;http://my.data.contract.namespace&amp;quot;&lt;/a&gt;&amp;quot; xmlns:i=&amp;quot;&amp;quot;&lt;a rel="nofollow" target="_new" href="http://www.w3.org/2001/XMLSchema-instance&amp;quot;&amp;quot;&amp;gt;"&gt;http://www.w3.org/2001/XMLSchema-instance&amp;quot;&amp;quot;&amp;gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;a:str&amp;gt;The string&amp;lt;/a:str&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/input&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/EchoDC&amp;gt;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;&amp;lt;/s:Body&amp;gt;&lt;/p&gt;
&lt;p&gt;&amp;lt;/s:Envelope&amp;gt;&amp;quot;;&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;byte[] reqBodyBytes = Encoding.UTF8.GetBytes(reqBody);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;req.GetRequestStream().Write(reqBodyBytes, 0, reqBodyBytes.Length);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;req.GetRequestStream().Close();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;HttpWebResponse resp = (HttpWebResponse)req.GetResponse();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(&amp;quot;HTTP/{0} {1} {2}&amp;quot;, resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (resp.ContentLength &amp;gt; 0)&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.WriteLine(new StreamReader(resp.GetResponseStream()).ReadToEnd());&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;resp.Close();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.Write(&amp;quot;Press ENTER to close the host&amp;quot;);&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Console.ReadLine();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;host.Close();&lt;/p&gt;
&lt;p&gt; &amp;nbsp; &amp;nbsp;}&lt;/p&gt;
&lt;p&gt;}&lt;/p&gt;
</description></item></channel></rss>