我正在通過快速入門來研究Prism v2。我已創建了以下簽名的WCF服務:從服務引用生成的類中解耦Silverlight客戶端
namespace HelloWorld.Silverlight.Web
{
[ServiceContract(Namespace = "http://helloworld.org/messaging")]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class HelloWorldMessageService
{
private string message = "Hello from WCF";
[OperationContract]
public void UpdateMessage(string message)
{
this.message = message;
}
[OperationContract]
public string GetMessage()
{
return message;
}
}
}
當我在我的Silverlight項目添加服務引用該服務將生成一個接口和一個類:
[System.ServiceModel.ServiceContractAttribute
(Namespace="http://helloworld.org/messaging",
ConfigurationName="Web.Services.HelloWorldMessageService")]
public interface HelloWorldMessageService {
[System.ServiceModel.OperationContractAttribute
(AsyncPattern=true,
Action="http://helloworld.org/messaging/HelloWorldMessageService/UpdateMessage",
ReplyAction="http://helloworld.org/messaging/HelloWorldMessageService/UpdateMessageResponse")]
System.IAsyncResult BeginUpdateMessage(string message, System.AsyncCallback callback, object asyncState);
void EndUpdateMessage(System.IAsyncResult result);
[System.ServiceModel.OperationContractAttribute(AsyncPattern=true, Action="http://helloworld.org/messaging/HelloWorldMessageService/GetMessage", ReplyAction="http://helloworld.org/messaging/HelloWorldMessageService/GetMessageResponse")]
System.IAsyncResult BeginGetMessage(System.AsyncCallback callback, object asyncState);
string EndGetMessage(System.IAsyncResult result);
}
public partial class HelloWorldMessageServiceClient : System.ServiceModel.ClientBase<HelloWorld.Core.Web.Services.HelloWorldMessageService>, HelloWorld.Core.Web.Services.HelloWorldMessageService {
{
// implementation
}
我試圖通過傳遞接口而不是具體類來解耦我的應用程序。但我很難找到如何做到這一點的例子。當我嘗試調用EndGetMessage然後更新我的UI時,我得到了關於在錯誤線程上更新UI的異常。我如何從後臺線程更新UI?
我試過但我得到UnauthorizedAccessException : Invalid cross-thread access
。
string messageresult = _service.EndGetMessage(result);
Application.Current.RootVisual.Dispatcher.BeginInvoke(() => this.Message = messageresult);
Application.Current.RootVisual
拋出異常。
我試過,但我得到UnauthorizedAccessException:無效的跨線程訪問。 string messageresult = _service.EndGetMessage(result); Application.Current.RootVisual.Dispatcher.BeginInvoke(()=> this.Message = messageresult ); Application.Current.RootVisual拋出異常 – 2009-03-06 01:20:12