WCF IGISServerConnection连接GIS server 问题

这段时间在写WCF,遇到一个很奇怪的问题,将WCF发布到IIS上后 IGISServerConnection.connet时总是不能连接。具体错误如下:

错误类型: 连接服务器;错误描述:拒绝访问。 (异常来自 HRESULT:0x80070005 (E_ACCESSDENIED))

Server stack trace: 
   在 System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
   在 System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   在 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)
   在 IDCIPLService.ff()

   在 DCIPLServiceClient.ff() 

 很奇怪 ,用vs直接运行时能连接上的,不会报错。期间各种网上说的方法都试过,还是不成功。后来改用ADF来连接,不再报错。

 

To make use of ArcObjects via ArcGIS Server in your application, you must connect directly to the GIS server, which requires network access to the Server Object Manager (SOM).  

ArcGIS Server Connection objects

You can connect to a GIS Server using the ArcGIS Server library (ESRI.ArcGIS.Server) or the ArcGIS Connection library (ESRI.ArcGIS.ADF.Connection ).

The Server library connects to the GIS server through the GISServerConnection object. The GISServerConnection object supports a single interface (in addition to IUnknown): IGISServerConnection.IGISServerConnection has a Connect method that connects the application to the GIS server.  The GISServerConnection object provides connections to the GIS server and access to the ServerObjectManagerand ServerObjectAdmin objects.   The identity of the connecting user is not explicitly defined.  Instead, the identity of the application is used.

The GISServerConnectionClass is a .NET generated wrapper for the GISServerConnection COM object.   

[C#]
ESRI.ArcGIS.Server.GISServerConnectionClass gisconnection; gisconnection = new ESRI.ArcGIS.Server.GISServerConnectionClass(); gisconnection.Connect("localhost"); ESRI.ArcGIS.Server.IServerObjectManager SOM = gisconnection.ServerObjectManager; 
  

The Connection library is a native .NET assembly with the same connection capabilities as the Server library, plus some additional management options.   In most cases, you should use the Connection library to connect to ArcGIS Server.  It connects to the GIS server through the AGSServerConnection class in the ESRI.ArcGIS.ADF.Connection assembly. The Connection library also contains a ConnectionManager class to manage fail-over and round-robin capabilities within the client.  See the section titled Using the Connection Manager for more information.

[C#]
ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity("user", "passwd", "domain"); ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconnection; agsconnection = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("hostname", identity); agsconnection.Connect(); IServerObjectManager SOM = agsconnection.ServerObjectManager;  
[VB.NET]
Dim identity As ESRI.ArcGIS.ADF.Identity = New ESRI.ArcGIS.ADF.Identity("user", "passwd", "domain")  Dim ags_onnection As ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconnection = New  ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("hostname", identity)  agsconnection.Connect  Dim SOM As IServerObjectManager = agsconnection.ServerObjectManager  

Connecting to the GIS Server: Security

For a client application to connect to ArcGIS Server, the application must be running as an operating system user that is a member of one of the following two operating system user groups defined on the ArcGIS server machines: the ArcGIS Server users group (agsusers) or ArcGIS Server administrators group (agsadmin). If the user the application is running as is not a member of either of those groups, then Connect will return an error. 

In addition to the Connect method, the IGISServerConnection interface has two properties: ServerObjectManager and ServerObjectAdmin. If the application is running as a user in the users or administrators group, the application can access the ServerObjectManager property, which returns an IServerObjectManager interface. The IServerObjectManager interface provides methods for accessing and creating objects within the server for use by applications.



To access the ServerObjectAdmin property, the application must be running as a user who is a member of the administrators group. If the connected user is not a member of this group, attempts to access the ServerObjectAdmin property will fail. The ServerObjectAdmin property returns the IServerObjectAdmin interface, which provides methods for administering the various aspects of the server, such as server object configurations and server machines. Unless you are writing a GIS server administration application, your application does not need to make use of the IServerObjectAdmin interface. 



The ServerObjectManager object provides methods for getting information about the GIS server and for creating server contexts for use by an application.


The ServerObjectAdmin object provides methods for administrating the GIS server and its server objects.



When connecting to the server using the native .NET connection object from a Web application or Web service, you must also think about impersonation. Your Web application or Web service must connect to the GIS server as a user in the agsusers group. The identity of the process or thread in determines the account you used to access the GIS Server.  Process identity is associated with the user account under which a process is running.  For ASP.NET Web applications or Web services you can impersonate a user at runtime by adding the identity tag to the web.config file and defining the appropriate attributes. 

<identity impersonate="true" userName="mydomain\myuser" password="mypassword" /> 						
If you do not use impersonation in an ASP.NET application, then it will attempt to connect to the GIS server using the identity of the ASP.NET worker process, which may be ASPNET or NETWORK SERVICE, depending on the platform and machine settings.  

Thread level impersonation is provided via the Connection library discussed earlier.  An Identity object is created with account credentials of a user in the agsusers group on the GIS Server.  When the AGSServerConnection is instantiated, it is provided the Identity object as a constructor parameter.   The identity is used for the duration of the connection, which may involve concurrent access from multiple threads in the same process.  The following code illustrates how a multi-threaded application creates concurrent connections to one or more GIS Servers using different identities: 

[C#]
public partial class _Default : System.Web.UI.Page {     private UserThread m_thread;      protected void Button1_Click(object sender, EventArgs e)     {         m_thread = new UserThread();         Thread t = new Thread(new ThreadStart(m_thread.Connection1));         t.Start();         Thread t2 = new Thread(new ThreadStart(m_thread.Connection2));         t2.Start();     } }  public class UserThread {     public void Connection1()     {         ESRI.ArcGIS.ADF.Identity id = new ESRI.ArcGIS.ADF.Identity("user1", "pass1", "domain1");         ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconn;         agsconn = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("server1", id);         agsconn.Connect();         if (!agsconn.IsConnected)         {             agsconn.Dispose();             return;         }         ESRI.ArcGIS.Server.IServerObjectManager som = agsconn.ServerObjectManager;         string servertype = "MapServer";         string serverobjectname = "Canada";         ESRI.ArcGIS.Server.IServerContext servercontext = som.CreateServerContext(serverobjectname, servertype);         IMapServer ms = (IMapServer) servercontext.ServerObject;          System.Threading.Thread.Sleep(10000);          servercontext.ReleaseContext();         agsconn.Dispose();     }      public void Connection2()     {         ESRI.ArcGIS.ADF.Identity id = new ESRI.ArcGIS.ADF.Identity("user2", "pass2", "domain2");         ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsconn;         agsconn = new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("server2", id);         agsconn.Connect();         if (!agsconn.IsConnected)         {             agsconn.Dispose();             return;         }         ESRI.ArcGIS.Server.IServerObjectManager som = agsconn.ServerObjectManager;         string servertype = "MapServer";         string serverobjectname = "USA";         ESRI.ArcGIS.Server.IServerContext servercontext = som.CreateServerContext(serverobjectname, servertype);         IMapServer ms = (IMapServer)servercontext.ServerObject;          servercontext.ReleaseContext();         agsconn.Dispose();     } } 

 

原文地址:https://www.cnblogs.com/lmyhao/p/2041193.html