gpt4 book ai didi

wcf - “无法从传输连接中读取数据 : An existing connection was forcibly closed by the remote host

转载 作者:行者123 更新时间:2023-12-04 08:36:50 26 4
gpt4 key购买 nike

我使用 Visual Studio 2010 和 MS SQL Server 2005。我是 WCF 的新手,我正在尝试在开发小型任务管理应用程序时学习。

目前我有一个解决方案,里面有两个控制台应用程序
- WCF 服务
- 连接到 WCF 服务以测试我的代码的客户端

WCF 服务使用 LINQ to SQL 连接到数据库。
我已经实现了一个 OperationContract,它将一些东西插入到 WCF 服务后面的数据库中并且它可以工作。
我已经实现了一个 OperationContract,它从 WCF 服务返回一个 GUID 到客户端并且它也可以工作。

当我尝试从 WCF 服务中检索 List 列表时,它失败并出现以下错误:

  • “接收到 localhost:8000/TaskerTest/Service/operations 的 HTTP 响应时发生错误。这可能是由于服务端点绑定(bind)未使用 HTTP 协议(protocol)。这也可能是由于服务器中止了 HTTP 请求上下文
  • InnerException:底层连接已关闭:接收时发生意外错误。

  • 我的契约(Contract)(前两个工作。GetLists 是问题):
    [ServiceContract(Namespace = "http://Tasker_Server")]
    public interface IOperations {
    [OperationContract]
    string CreateList(string listName, string text);
    [OperationContract]
    string UpdateList(int idList, string listName, string text);
    [OperationContract]
    List<ToDoList> 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;
    }

    数据库访问代码:
         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();

    我可以根据读者的需要提供任何额外的细节。

    更新

    我已经打开了跟踪
      <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 Service Trace Viewer 并收到以下错误:

    尝试序列化参数时出错。 InnerException 消息是 'Type 'System.DelegateSerializationHolder+DelegateEntry' 与数据协定名称 'DelegateSerializationHolder.DelegateEntry:http://schemas.datacontract.org/2004/07/System' 不是预期的

    我现在已将 [DataContract] 添加到我的 dbml 文件中的“ToDoList”类中​​,现在我没有收到异常。我在我的客户中得到了正确数量的结果,但是每个类的内容似乎都是空的。

    最佳答案

    我的数据库类是使用 dbml 文件(LINQ to SQL)自动生成的。

    在“TaskerModel.designer.cs”(自动生成的文件)中向 ToDoList 类添加 [DataContract()] 属性后,我不再收到异常,但我在客户端检索到的类只有默认值对于每个成员。

    然后我将 [DataMember()] 属性添加到 ToDoList 类的所有成员并添加

    [ServiceKnownType(typeof(ToDoList))]


        [ServiceContract(Namespace = "http://Tasker_Server")]
    public interface IOperations {
    [OperationContract]
    string CreateList(string listName, string text);
    [OperationContract]
    string UpdateList(int idList, string listName, string text);
    [OperationContract]
    List<ToDoList> GetLists();
    }

    现在它可以工作了。不知道是否有更简单的方法可以做到这一点,但它现在有效。如果有人知道更好的方法来做到这一点,我将不胜感激。

    关于wcf - “无法从传输连接中读取数据 : An existing connection was forcibly closed by the remote host,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10773654/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com