Instant DataContract action in javascript when consuming WCF service with ScriptService

I have a simple WCF service that I am browsing with .svc. The service has some associated DataContract classes that are used with ServiceContracts. When you call a method that receives some data, the returned json has all the properties as you would expect.

My question is how can I get a new instance of one of these DataContract objects in javascript.

<asp:ScriptManager ID="ScriptManager1" runat="server">
  <Services>
    <asp:ServiceReference 
      Path="./Service1.svc" />
  </Services>
</asp:ScriptManager>

<script>
// what I'd like to be able to do in javascript
var myInstance = new MyNamespace.MyDataContractClassName();
myInstance.someProperty = "Prop Value";
</script>

      

0


source to share


2 answers


If you go to. / Service 1.svc / js, you will see the exact client proxy script that is generated by the server.

At the end of this file, you should see lines that register the data contract types as client types - this just makes them available on the client type system and allows the well-known constructor to be called, but it doesn't actually encode their properties as you can set any arbitrary properties for any object in JavaScript.



So the code you actually wrote is correct assuming the namespace and class declaration match what is in the proxy code, but you can see for yourself.

+1


source


Thanks @Sam, I was hoping there was a way to create all the properties so that when creating a new MyDataContractClassname (); I could have a nice full object that I could traverse and shouldn't have to check for properties etc. - just treat it like an object I got from a service that returns a DataContract. If you say that I have a method that displays the content of that object to the user, and sometimes it shows a new one and sometimes it shows one of the service, it would be nice to just assume that all the properties are there, as you would if you were manually created "class"



0


source







All Articles