Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I manually create WCF OperationContract body type?
    primarykey
    data
    text
    <p>I'm trying to code a web service client in Silverlight for a RESTful WCF service that I have developed. In Silverlight I am constructing the body of the WebRequest using a DataContractSerializer instance.</p> <p>This approach works great if there is a single argument for the OperationContract. It doesn't work so well if there are multiple arguments defined in the OperationContract. I believe this is because a dynamic type is being created by WCF that is named after the OperationContract and the members of that type are named after the parameters defined for the operation. The purpose of the dynamic type is to ensure that a single XML element exists in the message body being submitted to the WCF service... makes sense. Question is how do I construct this dynamic type myself so that I can submit it to the DataContractSerializer myself.</p> <p>First example is a working example that defines a single parameter. Second example is the scenario that I am trying to solve (multiple parameters).</p> <hr> <p>Example 1:</p> <pre><code>[OperationContract, WebInvoke(Method = HttpMethodType.Post, BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "UnregisterProvider"), WebHelp(Comment = "Unregistered the provider type with the specified URI.")] void UnregisterProvider(RdfUri providerUri); </code></pre> <p>Code used to serialize message body:</p> <pre><code>StringBuilder msgBody = new StringBuilder(250); using (XmlWriter xw = XmlWriter.Create(msgBody)) { var serializer = new DataContractSerializer(typeof(RdfUri)); serializer.WriteObject(xw, providerUri); } </code></pre> <p>Resulting body:</p> <pre><code>&lt;RdfUri xmlns="http://schemas.datacontract.org/2004/07/Intellidimension.Rdf"&gt;esp:semanticserver&lt;/RdfUri&gt; </code></pre> <hr> <p>Example 2:</p> <pre><code>[OperationContract, WebInvoke(Method = HttpMethodType.Post, BodyStyle = WebMessageBodyStyle.WrappedRequest, /* WrappedRequest must somehow signal WCF to create the anonymous type as it is required for multiple parameter OperationContracts */ UriTemplate = "RegisterProvider"), WebHelp(Comment = "Registered a provider type with the specified URI.")] void RegisterProvider(PoolableEntityServiceProviderDescriptor descriptor, RdfUri providerUri); </code></pre> <p>Code used to serialize message body:</p> <pre><code>//????? </code></pre> <p>Resulting body:</p> <pre><code>&lt;RegisterProvider xmlns="http://tempuri.org/"&gt; &lt;descriptor i:type="a:SemanticServerProviderDescriptor" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.datacontract.org/2004/07/Intellidimension.RdfEntity.Service.DataContracts"&gt; &lt;a:ConnectionString&gt;Data Source=.\sqlexpress;Initial Catalog=RdfTest1;Persist Security Info=True;User ID=sa;Password=password&lt;/a:ConnectionString&gt; &lt;a:ProviderGraphUri&gt;http://entitystore/graph-provider&lt;/a:ProviderGraphUri&gt; &lt;/descriptor&gt; &lt;providerUri&gt;esp:semanticserver&lt;/providerUri&gt; &lt;/RegisterProvider&gt; </code></pre> <hr> <p><strong>Update 1</strong>:</p> <p>Here is a guy on the MSDN forums asking a similar question: <a href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/4bb22146-9972-418a-b81f-5102ea289af2/" rel="nofollow noreferrer">Can I use DataContractSerializerOperationFormatter to format a list of parameters from client to server?</a></p> <p>DataContractSerializerOperationFormatter is an internal class. So looks like I may have to implemented it's behavior for my client.</p> <p><strong>Update 2</strong>:</p> <p>Some are asking why I am not just using the normal Silverlight WCF client generated by a service reference. The reason is that the WCF service on the server is a RESTful service. From the <a href="http://msdn.microsoft.com/en-us/library/cc896571(VS.95).aspx" rel="nofollow noreferrer">docs</a>:</p> <blockquote> <p>No analog to the WebHttpBinding provided in WCF is provided. To access pure HTTP, REST, RSS/Atom, or AJAX services from Silverlight 2, use the techniques described in Accessing HTTP-Based Services Directly, such as the WebClient class. To access ASP.NET AJAX services, see Accessing ASP.NET AJAX Services.</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload