【发布时间】:2014-03-25 03:55:18
【问题描述】:
我尝试在单击按钮时使用 wcf 回调操作填充 datagridview,但在第一次单击时 wcf 返回空值,当我第二次单击按钮时,它会获取数据并填充 gridview 数据。
下面是我的 WCF 服务....
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract =typeof(IServiceCallback))]
public interface IService
{
[OperationContract(IsOneWay=true)]
void GetData(string userName,string password);
}
/// <summary>
/// The Callback Interface
/// </summary>
public interface IServiceCallback
{
[OperationContract(IsOneWay=true)]
void SendResult(Department[] arrDept);
}
[DataContract]
public class Department
{
[DataMember]
public int DeptNo { get; set; }
[DataMember]
public string DeptName { get; set; }
[DataMember]
public int Capacity { get; set; }
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerSession)]
public class Service : IService
{
/// <summary>
/// Get a channel instance that is used to called the current operation
/// </summary>
public IServiceCallback CallBack
{
get
{
return OperationContext.Current.GetCallbackChannel<IServiceCallback>();
}
}
public void GetData(string userName, string password)
{
Department[] arrDept = new Department[]
{
new Department() {DeptNo=10,DeptName="IT",Capacity=4500},
new Department() {DeptNo=20,DeptName="HRD",Capacity=200},
new Department() {DeptNo=30,DeptName="ACCTS",Capacity=40}
};
if (userName.Trim() == "mahesh" && password == "mahesh")
{
Thread.Sleep(10000);
CallBack.SendResult(arrDept);
}
}
}
Web.Config
<system.serviceModel>
<services>
<service behaviorConfiguration="Service"
name="WCF_CallBack_Service.Service">
<endpoint address="" binding="wsDualHttpBinding"
contract="WCF_CallBack_Service.IService">
</endpoint>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Service">
<serviceMetadata httpGetEnabled="True"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
客户呼叫...
public class RequestCallBack : IServiceCallback
{
private Department[] _Departments;
public Department[] Departments
{
get { return _Departments; }
set
{
_Departments = value;
}
}
public void SendResult(Department[] arrDept)
{
Departments = arrDept;
MessageBox.Show("Response Received " + Departments.Count().ToString() );
}
}
public partial class Form1 : Form
{
MyRef.ServiceClient Proxy;
RequestCallBack callback;
public Form1()
{
InitializeComponent();
}
private void btnGetData_Click(object sender, EventArgs e)
{
Proxy.GetData("mahesh", "mahesh");
MessageBox.Show("Values are send to the service");
dgvDept.DataSource = callback.Departments;
}
private void Form1_Load(object sender, EventArgs e)
{
callback = new RequestCallBack();
//Get the InstanceContext with the help of the CallBack contract class
InstanceContext context = new InstanceContext(callback);
Proxy = new MyRef.ServiceClient(context);
}
}
客户端 app.config..
<configuration>
<system.serviceModel>
<bindings>
<wsDualHttpBinding>
<binding name="WSDualHttpBinding_IService" />
</wsDualHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:4221/Service.svc" binding="wsDualHttpBinding"
bindingConfiguration="WSDualHttpBinding_IService" contract="MyRef.IService"
name="WSDualHttpBinding_IService">
<identity>
<userPrincipalName value="MY-PC\MY PC" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>
【问题讨论】: