These days in doing a Silverlight page chat tool, basically looked at Silverlight and WCF related things, but also encountered a lot of problems, just started with Silverlight did the timing of the server data information, late preparation to use WCF duplex communication To do, the communication process is divided into two parts records.
One, you need to build a WCF server
Here, a new WCF service is created directly in the Silverlight site, with the following code:
Define Service Contracts First
[ServiceContract]
public interface Ichatservice
{
[OperationContract]
BOOL SendMessage (Messageinfo msg);
[OperationContract]
List<messageinfo> getmessages (string useraccount);
}
Define data contracts again
[DataContract]
public class Messageinfo
{
[DataMember]
public string ID {set;
[DataMember]
public string Title {set;
[DataMember]
public string Message {set;
[DataMember]
Public DateTime Sendtime {set;
[DataMember]
Public DateTime? readtime {set; get;}
[DataMember]
public string Sender {set;
[DataMember]
public string Receiveuser {set;
[DataMember]
public string Receiveorgan {set;
[DataMember]
public string Receivemode {set;
[DataMember]
public int State {set; get;}
[DataMember]
public int Receipt {set; get;}
[DataMember]
public string Source {set;
}
Finally, configure the service in the Webconfig
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name= "Chatbehavior" >
<servicemetadata httpgetenabled= "true"/>
<servicedebug includeexceptiondetailinfaults= "false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorconfiguration= "Chatbehavior" name= "WebChat.Web.ChatService" >
<endpoint address= "" binding= "BasicHttpBinding" contract= "WebChat.Web.IChatService" ></endpoint>
</service>
</services>
<servicehostingenvironment multiplesitebindingsenabled= "true"/>
</system.serviceModel>