System.ServiceModel.FaultException`1:'会话已关闭。请再次登录'(C#网络服务)

时间:2017-08-16 16:13:19

标签: c# web-services console-application

我已经创建了一个Web服务,即使它连接并从响应方法中提取数据,它也会挂起这个错误:

  

System.ServiceModel.FaultException`1:'会话已关闭。请再次登录'

enter image description here

当我进入调试步骤时,这部分代码将其设置为关闭:

public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        //Get the HttpRequestMessage property from the message
        HttpRequestMessageProperty httpreq = null;

        try
        {
            httpreq = request.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty;
        }
        catch //(Exception exc)
        {
        }

        if (httpreq == null)
        {
            httpreq = new HttpRequestMessageProperty();
            request.Properties.Add(HttpRequestMessageProperty.Name, httpreq);
        }

        byte[] authbytes = Encoding.UTF8.GetBytes(string.Concat(Username, ":", Password));
        string base64 = Convert.ToBase64String(authbytes);
        string authorization = string.Concat("Basic ", base64);

        httpreq.Headers["Authorization"] = authorization;

        return null;
    }

有没有人知道如何在C#控制台应用中杀死会话以确保不会继续发生这种情况?

谢谢!

1 个答案:

答案 0 :(得分:0)

别介意这个问题。此问题即将发生,因为我的代码依赖于另一个Web会话来建立连接。这就是我收到“会话已关闭”错误的原因,因为我在没有打开其他服务的情况下运行该项目。

谢谢。