[英]How to properly call an external WCF Service from SharePoint 2010?
I would like to call an external WCF Service from within SharePoint.我想从 SharePoint 中调用外部 WCF 服务。 Using normal WCF calls immediately causes a
SocketException
(An existing connection was forcibly closed by the remote host), which can be circumvented with SPSecurity.RunWithElevatePrivileges
.使用正常的 WCF 调用会立即导致
SocketException
(现有连接被远程主机强制关闭),可以使用SPSecurity.RunWithElevatePrivileges
规避。
[SocketException (0x2746): An existing connection was forcibly closed by the remote host]
[SocketException (0x2746): 现有连接被远程主机强行关闭]
System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +85System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) +85
System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing) +204System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] 缓冲区,Int32 偏移量,Int32 大小,TimeSpan 超时,Boolean 关闭)+204
[CommunicationException: The socket connection was aborted.
[CommunicationException:套接字连接被中止。 This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.
这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题造成的。 Local socket timeout was '00:00:58.7210000'.]
本地套接字超时为 '00:00:58.7210000'。]
System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout, Boolean closing) +15307563System.ServiceModel.Channels.SocketConnection.ReadCore(Byte[] 缓冲区,Int32 偏移量,Int32 大小,TimeSpan 超时,Boolean 关闭)+15307563
System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +90System.ServiceModel.Channels.SocketConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +90
System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +34System.ServiceModel.Channels.DelegatingConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +34
System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout) +34System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count, TimeSpan timeout) +34
System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count) +88System.ServiceModel.Channels.ConnectionStream.Read(Byte[] buffer, Int32 offset, Int32 count) +88
System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) +58System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count) +58
System.Net.Security.NegotiateStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +62System.Net.Security.NegotiateStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +62
System.Net.Security.NegotiateStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +54System.Net.Security.NegotiateStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +54
System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +402System.Net.Security.NegotiateStream.ProcessRead(Byte[] 缓冲区,Int32 偏移量,Int32 计数,AsyncProtocolRequest asyncRequest) +402
[IOException: The read operation failed, see inner exception.]
[IOException:读取操作失败,请参阅内部异常。]
System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +704System.Net.Security.NegotiateStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest) +704
System.Net.Security.NegotiateStream.Read(Byte[] buffer, Int32 offset, Int32 count) +154System.Net.Security.NegotiateStream.Read(Byte[] buffer, Int32 offset, Int32 count) +154
System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +87System.ServiceModel.Channels.StreamConnection.Read(Byte[] buffer, Int32 offset, Int32 size, TimeSpan timeout) +87
[CommunicationException: The socket connection was aborted.
[CommunicationException:套接字连接被中止。 This could be caused by an error processing your message or a receive timeout being exceeded by the remote host, or an underlying network resource issue.
这可能是由于处理您的消息时出错或远程主机超出接收超时,或者是潜在的网络资源问题造成的。 Local socket timeout was '00:00:58.7210000'.]
本地套接字超时为 '00:00:58.7210000'。]
System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10257978System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) +10257978
System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) +539System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 类型) +539
MyApp.FunctionThatCallsService()MyApp.FunctionThatCallsService()
That got me thinking: SharePoint does it, and they do it in a nice way where they have individual service configurations in 14\WebClients
.这让我想到: SharePoint 做到了,而且他们做得很好,他们在
14\WebClients
中有单独的服务配置。
I reflected and cloned a lot of it (The Extension Methods on the ChannelFactory are used internally all over the place - SPChannelFactoryOperations ), but I wonder: Are the "proper" APIs that SharePoint uses to call it's own WCF Services exposed to external code, so that I can call my non-SharePoint WCF Services from custom SharePoint code?我反映并克隆了很多(ChannelFactory 上的扩展方法在内部到处都在使用 - SPChannelFactoryOperations ),但我想知道:SharePoint 用来调用它自己的 WCF 服务的“正确”API 是否暴露给外部代码,以便我可以从自定义 SharePoint 代码调用我的非 SharePoint WCF 服务?
(This is a farm solution deployed to the GAC, so neither CAS nor Sandboxing applies) (这是部署到 GAC 的场解决方案,因此 CAS 和沙盒都不适用)
Ended up keeping the SPSecurity.RunWithElevatedPrivileges
section.最终保留了
SPSecurity.RunWithElevatedPrivileges
部分。 Microsoft's SPChannelFactoryOperations
at some point hit a NullReferenceException, most likely due to Claims required to be setup within the service even when not using it ( SPServiceAuthenticationMode
only has SPServiceAuthenticationMode.Claims
as a member. The fact that CreateChannelActingAsLoggedOnUser()
immediately throws an ArgumentException is a signal that the SPChannelFactoryOperations
aren't meant to be used by non-Microsoft code. Microsoft 的
SPChannelFactoryOperations
在某些时候遇到 NullReferenceException,很可能是由于即使不使用它也需要在服务中设置声明( SPServiceAuthenticationMode
只有SPServiceAuthenticationMode.Claims
作为成员CreateChannelActingAsLoggedOnUser()
立即抛出 ArgumentException 的事实是一个信号SPChannelFactoryOperations
不打算由非 Microsoft 代码使用。
I recommend trying to ignore certificates.我建议尝试忽略证书。
http://www.fryan0911.com/2006/07/vbnet-how-to-accept-web-service-ssl.html http://www.fryan0911.com/2006/07/vbnet-how-to-accept-web-service-ssl.html
Perhaps you need a proxy?也许您需要代理? Are you having some type of port block problem?
您是否遇到某种类型的端口阻塞问题?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.