用C++写了个DLL库 封装了一个别人写的API库我用C#写个winform程序可以正常使用这个DLL可我用WCF调用这个DLL时却报错 接收对 http://localhost:51782/Service1.svc 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。
我把C++中所有涉及到别人的API函数都删除就不会出错了这个是什么原因呢?  线程的关联性么?

解决方案 »

  1.   

    [DllImport("My.dll")]//这是我自己定义的某个动态库  
      public extern int Mycall(int);//这是动态库里的一个函数  
    这段放在CalculatorService 外
      

  2.   

    我知道怎么调用DLL  请仔细看问题谢谢
      

  3.   

    看样子还是你的WCF配置不正确导致的。你先简单的创建一个WCF调用看看吧。
      

  4.   

    简单的没问题 WCF下 在我DLL里只要加了别人开发的那些API函数就不行 
    但是在WINFORM的程序下 却没问题 
      

  5.   

    Website和winform,对于platform invoke的影响,我能想到的是
    1. 运行账户不同造成的权限问题。
       winform是当前登录用户,IIS website是network service
       不过看你使用的应该是vs 的web server,应该不是这个问题2. 当前路径不同
       winform的当前路径就是exe所在的路径,而asp.net的当前路径是C:\windows\system32 好像是这个,方正不是你web site的bin目录
        可以将dll copy到%windir%\system32\试一试。
      

  6.   

    对于当前路径问题还有个solution是将bin目录降到Path环境变量中去, 在调用之前。static void AddEnvironmentPaths(string[] paths)
    {
        string path = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
        path += ";" + string.Join(";", paths);    Environment.SetEnvironmentVariable("PATH", path);
    }
      

  7.   

    昨天我也想过可能是因为DLL以及别人写的API DLL相关依赖库找不到这个问题 所以也做了些测试测试的结果是别人写的API函数 正常返回  我的DLL函数也正常返回  但是在WCF函数返回时报上述的错误
     [OperationContract]
            public void GetSnapValue(string PtName)
            {
                Interface_init("172.21.130.59", 0, 1);//我的DLL接口函数 此函数中没有调用别
    //人的API 所以如果只调用这个 不调用GetSnapshotValue的话 WCF可以正常返回
                int dd = 10;
                float ff = 12;
                short ss = 12;
                int Res = GetSnapshotValue("1RCP010VE.av", ref ff, ref ss);//此函数内调用了
    //别人写的API函数   且此函数可以正常返回
           }//在这里WCF返回的时候会报错
      

  8.   

    这里我只为测试 所以WCF函数本身并没有返回值 这样可以排除序列化引发的问题
      

  9.   

    从错误看,好像是asp.net的进程死掉了。你再GetSnapshotValue后面在加些dummy语句,看看能不能继续执行。GetSnapshotValue是同步的吗?我的意思是GetSnapshotValue里面是不是启动什么线程,进程什么的了?
    因此虽然GetSnapshotValue返回了,但是里面的线程造成process死掉了。如果有可能的话拿到GetSnapshotValue的源代码和pdb,进行跟踪下最好了。
      

  10.   

    GetSnapshotValue 是我自己写的 里面封装了些别人写的API函数  别人的API代码我拿不到
    查了些资料  也猜测可能是WCF的线程关联性 可能是关联到人家的API后台线程去了  大概由于人家后台线程池中线程销毁而销毁了网上找了写取消关联性的方法
    如修改参数 ServiceBehavior(UseSynchronizationContext = false)
    或者[CallbackBehavior(UseSynchronizationContext = false)]
    没有用处PS:  c# 和wcf纯属突击学习使用 还不熟 您说的dummy语句是什么东东?怎么使  没查到啊
      

  11.   


    [OperationContract]
            public void GetSnapValue(string PtName)
            {
                Interface_init("172.21.130.59", 0, 1);//正常执行
                int dd = 10; //正常执行
                float ff = 12;   //正常执行         
                short ss = 12;//正常执行
                int Res = GetSnapshotValue("1RCP010VE.av", ref ff, ref ss);////正常执行
                ss = 18;//正常执行
                ff = ss * 2;//正常执行
              }////执行后报错
    //错误内容未能调用服务。可能的原因: 服务已脱机或无法访问;客户端配置与代理不匹配;现有的代理无
    //效。有关详细信息接收对 http://localhost:51782/Service1.svc 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。有关详细信息,请参见服务器日志。Server stack trace: 
       在 System.ServiceModel.Channels.HttpChannelUtilities.ProcessGetResponseWebException(WebException webException, HttpWebRequest request, HttpAbortReason abortReason)
       在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
       在 System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout)
       在 System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout)
       在 System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
       在 System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
       在 System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)Exception rethrown at [0]: 
       在 System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
       在 System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
       在 Service1.GetSnapValue(String PtName)
       在 Service1Client.GetSnapValue(String PtName)Inner Exception:
    基础连接已经关闭: 接收时发生错误。
       在 System.Net.HttpWebRequest.GetResponse()
       在 System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)Inner Exception:
    无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。
       在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       在 System.Net.PooledStream.Read(Byte[] buffer, Int32 offset, Int32 size)
       在 System.Net.Connection.SyncRead(HttpWebRequest request, Boolean userRetrievedStream, Boolean probeRead)Inner Exception:
    远程主机强迫关闭了一个现有的连接。
       在 System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
       在 System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)