WCF Service invalid with Silverlight
I'm trying to get WCF working with Silverlight. I'm a total beginner to WCF but have written asmx services in the past. For some reason when I uncomment more than one method in my service Silverlight refuses to let me use it, saying it is invalid. My code is below if anyone could help. I'm using the Entity Framework if that makes a difference.
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompati开发者_JAVA技巧bilityRequirementsMode.Allowed)]
public class MessageService {//: IMessageService {
/// <summary>
/// Sends a new message.
/// </summary>
/// <param name="recipientUsername">The recipient username.</param>
/// <param name="subject">The subject.</param>
/// <param name="messageBody">The message body.</param>
[OperationContract]
public void SendMessageByDetails(string recipientUsername, string subject, string messageBody) {
MessageDAL.SendMessage(recipientUsername, subject, messageBody);
}
/// <summary>
/// Sends a new message.
/// </summary>
/// <param name="msg">The message to send.</param>
[OperationContract]
public void SendMessage(Message msg) {
MessageDAL.SendMessage(msg);
}
}
You need to have a [ServiceContract]
attribute on the class, or it needs to implement an interface that has a [ServiceContract]
attribute and the service methods defined there marked up with [OperationContract]
attributes.
You may find that creating a simple "grey screen" Windows Forms application makes it easier to diagnose issues like this.
If you're just using WCF to get some data into Silverlight it may be worth looking at WCF RIA Services which constructs the WCF service for you end exposes your entities to your client side Silverlight.
What does your service reference configuration look like? Remember that SilverLight can only make use of WCF services using http binding.
精彩评论