如何从SharePoint 2010正确调用外部WCF服务?

Mic*_*tum 10 .net c# sharepoint sharepoint-2010

我想从SharePoint中调用外部WCF服务.使用普通的WCF调用会立即导致SocketException(现有连接被远程主机强行关闭),可以使用SPSecurity.RunWithElevatePrivileges来规避它.

[SocketException(0x2746):远程主机强制关闭现有连接]
System.Net.Sockets.Socket.Receive(Byte []缓冲区,Int32偏移量,Int32大小,SocketFlags socketFlags)+85
System.ServiceModel.Channels.SocketConnection .ReadCore(Byte [] buffer,Int32 offset,Int32 size,TimeSpan timeout,Boolean closing)+204

[CommunicationException:套接字连接已中止.这可能是由于处理消息的错误或远程主机超出接收超时或基础网络资源问题引起的.本地套接字超时为'00:00:58.7210000'.]
System.ServiceModel.Channels.SocketConnection.ReadCore(Byte [] buffer,Int32 offset,Int32 size,TimeSpan timeout,Boolean closing)+15307563
System.ServiceModel.Channels.SocketConnection.读取(Byte []缓冲区,Int32偏移量,Int32大小,TimeSpan超时)+90
System.ServiceModel.Channels.DelegatingConnection.Read(Byte []缓冲区,Int32偏移量,Int32大小,TimeSpan超时)+34
System.ServiceModel.Channels. ConnectionStream.Read(Byte [] buffer,Int32 offset,Int32 count,TimeSpan timeout)+34
System.ServiceModel.Channels.ConnectionStream.Read(Byte [] buffer,Int32 offset,Int32 count)+88
System.Net.FixedSizeReader.ReadPacket (Byte [] buffer,Int32 offset,Int32 count)+58
System.Net.Security.NegotiateStream.StartFrameHeader(Byte [] buffer,Int32 offset,Int32 count,AsyncProtocolRequest asyncRequest)+62
System.Net.Security.NegotiateStream.StartReading(字节[]缓冲区,Int32偏移量,Int32计数,AsyncProtocolRequest asyncRequest)+54
System.Net.Security.N egotiateStream.ProcessRead(Byte [] buffer,Int32 offset,Int32 count,AsyncProtocolRequest asyncRequest)+402

[IOException:读取操作失败,请参阅内部异常.]
System.Net.Security.NegotiateStream.ProcessRead(Byte [] buffer,Int32 offset,Int32 count,AsyncProtocolRequest asyncRequest)+704
System.Net.Security.NegotiateStream.Read(Byte) [] buffer,Int32 offset,Int32 count)+154
System.ServiceModel.Channels.StreamConnection.Read(Byte [] buffer,Int32 offset,Int32 size,TimeSpan timeout)+87

[CommunicationException:套接字连接已中止.这可能是由于处理消息的错误或远程主机超出接收超时或基础网络资源问题引起的.本地套接字超时为'00:00:58.7210000'.]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg,IMessage retMsg)+10257978
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData&msgData,Int32 type )+539
MyApp.FunctionThatCallsService()

这让我想到了:SharePoint做到了,他们以一种很好的方式做到了,他们在14\WebClients中有各自的服务配置.

我反思并克隆了很多内容(ChannelFactory上的扩展方法在内部使用 - SPChannelFactoryOperations),但我想知道:SharePoint使用的"适当"API来调用它自己的外部代码暴露的WCF服务,以便我可以从自定义SharePoint代码调用我的非SharePoint WCF服务?

(这是部署到GAC的服务器场解决方案,因此CAS和沙盒都不适用)

Mic*_*tum 2

最终保留了该SPSecurity.RunWithElevatedPrivileges部分。MicrosoftSPChannelFactoryOperations在某些时候遇到了 NullReferenceException,很可能是由于即使不使用它(SPServiceAuthenticationMode仅作为SPServiceAuthenticationMode.Claims成员)也需要在服务中设置声明。CreateChannelActingAsLoggedOnUser()立即抛出 ArgumentException 的事实表明,这些声明SPChannelFactoryOperations并不意味着由非 Microsoft 代码使用。