我使用Visual Studio 2010和MS SQL Server 2005.我是WCF的新手,我试图在开发时学习小的任务管理应用“无法从传输连接读取数据:现有连接被远程主机强制关闭
目前,我在它里面 有两个控制台应用的解决方案 - WCF服务 - 一个客户端连接到WCF服务来测试我的代码
的WCF服务连接到一个使用LINQ to SQL的数据库 我已经实现了一个OperationContract,它在WCF服务后面的数据库中插入了一些东西,它可以工作。我已经实现了一个OperationContract,它从WCF服务返回一个GUID到客户端,这也起作用。
当我尝试失败,出现以下错误的WCF服务检索目录的列表:8000/TaskerTest /服务/运营:在接收到本地主机的HTTP响应
“时发生错误。这可能是由于服务端点绑定不使用HTTP协议造成的,这也可能是由于HTTP请求上下文被服务器中止造成的
InnerException:底层连接已关闭:发生意外错误收到。
我的合同(前两项工作。 GetLists的问题):
实施:
public List<ToDoList> GetLists() {
Guid token = OperationContext.Current.IncomingMessageHeaders.GetHeader<Guid>("token", "ns");
int? userID = CheckCache(token);
if (userID != null) {
List<ToDoList> lst = DBAccess.GetListsByUserId(userID.Value);
return lst;
}
else
return null;
}
DB访问代码:
public static List<ToDoList> GetListsByUserId(int idCredential) {
TaskerModelDataContext tmdc = new TaskerModelDataContext();
List<ToDoList> lists = tmdc.ToDoLists.Where(entry => entry.id_Credential == idCredential).ToList<ToDoList>();
return lists;
}
而且客户端代码(WCF服务添加为服务引用客户端项目):
TaskerService.LoginClient test2 = new LoginClient();
string username = "usr1", password = "psw1";
test2.ClientCredentials.UserName.UserName = username;
test2.ClientCredentials.UserName.Password = password;
Guid result = Guid.Empty;
try {
result = test2.CheckCredentials(username, password);
Console.WriteLine("Login OK!");
}
catch (Exception e) {
Console.WriteLine(e.Message);
}
TaskerService.OperationsClient client = new OperationsClient();
using(OperationContextScope contextScope = new OperationContextScope(client.InnerChannel)) {
Guid testToken = result; Console.WriteLine(testToken.ToString());
MessageHeader<Guid> mhg = new MessageHeader<Guid>(testToken);
MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
client.GetLists();
}
client.Close();
客户端失败,出现以上异常:client.GetLists();
我可以根据读者的需要提供任何其他细节。
UPDATE
我已经变成追踪与
<system.diagnostics>
<trace autoflush="true" />
<sources>
<source name="System.ServiceModel"
switchValue="Information, ActivityTracing"
propagateActivity="true">
<listeners>
<add name="sdt"
type="System.Diagnostics.XmlWriterTraceListener"
initializeData= "SdrConfigExample.e2e" />
</listeners>
</source>
</sources>
</system.diagnostics>
然后我在生成的日志运行Microsoft服务跟踪查看,并得到了以下错误:
有尝试序列化参数时出错。 InnerException消息是'类型'System.DelegateSerializationHolder + DelegateEntry'与数据合同名称'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System'不是预期的
我已经添加[DataContract]到我的dbml文件中的“ToDoList”类,现在我没有收到异常。我在客户端得到了正确数量的结果,但每个类的内容似乎都是空的。