WCF和WPF绑定问题 [英] WCF and WPF Binding Issue

查看:80
本文介绍了WCF和WPF绑定问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在应用程序中使用WCF.我们提供的服务是Singleton服务.现在,当我从客户端进行服务调用时,我想知道客户端中的每个服务调用是否来自不同的线程?如果可以,我是否可以在单个线程中进行所有这些调用?

I am using WCF in my application. The service we have is a Singleton service. Now when I make a service call from my client, I would like to know if each of these Service call from client is from different Thread? If yes is there a way for me to make each of these calls in a single thread?

在我们的应用程序中实现的服务是Singleton服务.从我们的客户那里,我们只有2个服务调用,即CreateData()和OpenData().
因此,当我们进行服务调用时,将在服务器中创建CreateData()对象,而当我们进行服务调用OpenData()时,将访问相同的对象.

Service implemented in our application is a Singleton service. From our client we have only 2 service call i.e. CreateData() and OpenData().
So when we make a service call CreateData() objects are created in the server and same is accessed when we make service call OpenData().

现在,当我们调用CreateData()服务时,我们从数据库中获取了一些数据并更新了一些称为"DBObject"的对象.现在,在此之后,我们创建另一个对象"BindObject".我们在其中绑定来自"BDObject"的数据进入"BindObject"使用WPF绑定.因此,当我们将数据绑定到BindObject时,此对象有一个Dispatcher对象,该对象具有当前线程的引用(现在,如果我检查System.Threading.Thread.CurrentThread.ManagedThreadId它将给我值为13).
现在我制作"OpenData()";服务调用,在这里我尝试访问BindObject,然后收到错误消息,即调用线程无法访问该对象,因为其他线程拥有它".所以我检查了System.Threading.Thread.CurrentThread.ManagedThreadId,它是19.

Now when we make a call to CreateData() service , we fetch some data from the Database and update some object called "DBObject". Now after this we create another object "BindObject" where we bind the data from the "BDObject" in to "BindObject" using the WPF binding. So when we bind the data to BindObject, this object has a Dispatcher object which has a reference of the Current Thread (Now if I check for System.Threading.Thread.CurrentThread.ManagedThreadId it gives me the value as 13).
Now I make "OpenData()" service call, here I try to access the BindObject, then I get an error i.e. "The calling thread cannot access this object because a different thread owns it". So I checked the System.Threading.Thread.CurrentThread.ManagedThreadId and it was 19.

因此,从不同线程访问时,访问使用数据绑定绑定的数据是一个问题.所以我尝试了Dispatcher.BeginInvoke(delegate method,params),但是没有调用委托方法.

So accessing the data which is bound using the Data Binding is an issue when accessed from different thread. So I tried Dispatcher.BeginInvoke(delegate method, params) , but the delegate method is not getting called.

请告诉我需要做什么?

预先感谢.

推荐答案

我试图模拟您在说什么(检查如果是您上面提到的内容),则如下:

[ {
CompositeType CompositeType = null <字体样式="font-size:10px">;
公共 int 值)
{
compositeType =
();
compositeType.StringValue = "Phani"" ;
compositeType.BoolValue =
;
返回 ,值);
}

I tried to Simulate what you were saying (check if its what you have mentioned above) below:

[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class Service1 : IService1
{
CompositeType compositeType=null;
public string GetData(int value)
{
    compositeType =
new CompositeType();
    compositeType.StringValue =
"Phani";
    compositeType.BoolValue =
true;
    return string.Format("You entered: {0}", value);
}

公共 <字体样式="font-size:10px" color =#2b91af"> CompositeType GetDataUsingDataContract( CompositeType 复合)
{
font>
返回 CompositeType;
}

public CompositeType GetDataUsingDataContract(CompositeType composite)
{
    
return compositeType;
}

}

只需为以下内容修改默认服务实现我们得到的WCF库.然后我们得到正确的值.当您运行此实现时,Phani是正确的.在GetData中,我们实际上设置了一个abject并使用GetDataUsingDataContext访问该对象.

能否请您在上述代码行中复制一些相关的代码行.
------------------------
请将该帖子标记为已回答确实...


这篇关于WCF和WPF绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆