服务器拒绝对自托管服务的请求

发布于 2024-12-09 12:35:01 字数 5955 浏览 1 评论 0原文

我创建了一个带有代理的自托管服务 服务和合约

Namespace ECDBDatabase.Service
Public Class DatabaseService
    <ServiceContract(Name:="DatabaseService",      Namespace:="net.tcp://localhost:9010/ECDBDatabase.Service")> _
    Public Interface IRPMSDatabaseService
<OperationContract()> _
        Function GetHandover(ByVal Username As String, ByVal Password As String) As DataSet
End Interface

  Public Function GetHandover(ByVal Username As String, ByVal Password As String) As DataSet Implements IRPMSDatabaseService.GetHandover
            'Connection string to Database
            Dim ReadConnectionString As String = "data source =localhost;" + "User ID=" & Username + ";Password=" & Password + ";Database=somedatabase"
            ReadConnection = New SqlConnection(ReadConnectionString)
            'Fill and return the dataset
            Try
                dbScriptFile = "some sql file"
                objReader = New StreamReader(dbScriptFile)
                cmd.CommandText = objReader.ReadToEnd.Replace("Go", ";")
                scriptArr = cmd.CommandText.Split(";")
                cmd.Connection = ReadConnection
                HandoverDataset = New DataSet
                HandoverAdapter = _
                    New SqlDataAdapter(cmd)

                For i = 0 To scriptArr.Length - 1
                    cmd.CommandText = scriptArr.GetValue(i)
                    'Fill the dataset
                    HandoverAdapter.Fill(HandoverDataset)
                Next
                'Return the dataset.

                Return HandoverDataset
            Catch ex As Exception
                Throw ex
            End Try
        End Function
        End Class
       End Class
End Namespace

我的主机如下:

 Sub Main()
    'Instantiate the service address
    Dim baseAddress As Uri = New Uri("net.tcp://localhost:9010/ECDBDatabase.Service")
    'Create the servicehost
    Using ECDBHost As New ServiceHost(GetType(ECDBService.ECDBDatabase.Service.DatabaseService.RPMSDatabaseService), baseAddress)

        Dim smb As New ServiceMetadataBehavior
        Dim debug As New ServiceDebugBehavior
        smb.HttpGetEnabled = False
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
        debug.IncludeExceptionDetailInFaults = True

        ECDBHost.Description.Behaviors.Add(smb)

        ECDBHost.Open()

        'Execute commands on console application
        Console.WriteLine("Service has started at {0}", baseAddress)
        Console.ReadLine()

    End Using
End Sub

我的代理如下;

 Public Function GetHandover(ByVal UserName As String, ByVal Password As String) As DataSet
        Try
            HandoverDataset = New DataSet

            tempBinding = New NetTcpBinding()
            tempAddress = New EndpointAddress(New Uri("net.tcp://localhost:9010/ECDBDatabase.Service"), New SpnEndpointIdentity(""))
            With tempBinding

            End With
            With tempAddress

            End With
            'Instantiating the channel for the proxy and setting the proxy up to communicate
            tempFactory = New ChannelFactory(Of ECDBService.ECDBDatabase.Service.DatabaseService.IRPMSDatabaseService)(tempBinding, tempAddress)
            With tempFactory
                tempProxy = .CreateChannel()
            End With
            'Setting the contracts to the channel
            With tempProxy
                HandoverDataset = .GetHandover(UserName, Password)

            End With
            Return HandoverDataset
        Catch ex As Exception
            Throw ex
        End Try
    End Function

我调用代理,然后代理访问服务,如下所示:

 Private Sub frmHandover_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Load the dataset and bind the controls.
    Try
        HandoverData = New DatabaseProxyClass.ECDBDatabase.Service.DatabaseProxy

        User = frmEhawkRPMS.UserN
        Pass = frmEhawkRPMS.PassW
        HandoverSet = New DataSet
        HandoverSet = HandoverData.GetHandover(User, Pass)

当我尝试从代理访问服务时,我的问题开始了,我收到以下错误:“数据错误:服务器无法处理请求,因为内部错误。 有关错误的更多信息,请在服务器上打开 IncludeExceptionDetailInFaults(从 servicebahviorattribute 或从配置行为),以便将异常信息发送回客户端,或者根据 Microsoft.net Framework 3.0 SDK 文档打开跟踪并检查服务器跟踪日志。” 我尝试启用异常,但收到一个错误,指出已经存在服务行为,并且不允许我将异常详细信息添加到服务行为中,并且没有太多(如果有的话)将其添加到 self 中的示例。用 VB 托管 WCF。 我正在寻找一些关于在 VB 中添加异常的建议,或者如果有人注意到我的问题是什么并且可以指出它,我也会非常感激。 这也是我的应用程序配置,位于启动服务的控制台应用程序中。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
<compilation debug="true" />
</system.web>
 <system.serviceModel>
   <services>
     <service name="ECDBService.ECDBDatabase.Service.RPMSDatabaseService">
       <endpoint address="net.tcp://localhost:9010/ECDBDatabase.Service"
              binding="netTcpBinding"
              contract="ECDBService.ECDBDatabase.Service.IRPMSDatabaseService" />
        <endpoint address ="" binding="wsHttpBinding"         contract="ECDBService.ECDBDatabase.Service.RPMSDatabaseService">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <!-- Metadata Endpoints -->
    <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
    <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
    </services>     
  </system.serviceModel>
 </configuration>

I have created a self hosted service with a proxy
Service and Contracts

Namespace ECDBDatabase.Service
Public Class DatabaseService
    <ServiceContract(Name:="DatabaseService",      Namespace:="net.tcp://localhost:9010/ECDBDatabase.Service")> _
    Public Interface IRPMSDatabaseService
<OperationContract()> _
        Function GetHandover(ByVal Username As String, ByVal Password As String) As DataSet
End Interface

  Public Function GetHandover(ByVal Username As String, ByVal Password As String) As DataSet Implements IRPMSDatabaseService.GetHandover
            'Connection string to Database
            Dim ReadConnectionString As String = "data source =localhost;" + "User ID=" & Username + ";Password=" & Password + ";Database=somedatabase"
            ReadConnection = New SqlConnection(ReadConnectionString)
            'Fill and return the dataset
            Try
                dbScriptFile = "some sql file"
                objReader = New StreamReader(dbScriptFile)
                cmd.CommandText = objReader.ReadToEnd.Replace("Go", ";")
                scriptArr = cmd.CommandText.Split(";")
                cmd.Connection = ReadConnection
                HandoverDataset = New DataSet
                HandoverAdapter = _
                    New SqlDataAdapter(cmd)

                For i = 0 To scriptArr.Length - 1
                    cmd.CommandText = scriptArr.GetValue(i)
                    'Fill the dataset
                    HandoverAdapter.Fill(HandoverDataset)
                Next
                'Return the dataset.

                Return HandoverDataset
            Catch ex As Exception
                Throw ex
            End Try
        End Function
        End Class
       End Class
End Namespace

My host is as follows:

 Sub Main()
    'Instantiate the service address
    Dim baseAddress As Uri = New Uri("net.tcp://localhost:9010/ECDBDatabase.Service")
    'Create the servicehost
    Using ECDBHost As New ServiceHost(GetType(ECDBService.ECDBDatabase.Service.DatabaseService.RPMSDatabaseService), baseAddress)

        Dim smb As New ServiceMetadataBehavior
        Dim debug As New ServiceDebugBehavior
        smb.HttpGetEnabled = False
        smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15
        debug.IncludeExceptionDetailInFaults = True

        ECDBHost.Description.Behaviors.Add(smb)

        ECDBHost.Open()

        'Execute commands on console application
        Console.WriteLine("Service has started at {0}", baseAddress)
        Console.ReadLine()

    End Using
End Sub

My Proxy is as follows;

 Public Function GetHandover(ByVal UserName As String, ByVal Password As String) As DataSet
        Try
            HandoverDataset = New DataSet

            tempBinding = New NetTcpBinding()
            tempAddress = New EndpointAddress(New Uri("net.tcp://localhost:9010/ECDBDatabase.Service"), New SpnEndpointIdentity(""))
            With tempBinding

            End With
            With tempAddress

            End With
            'Instantiating the channel for the proxy and setting the proxy up to communicate
            tempFactory = New ChannelFactory(Of ECDBService.ECDBDatabase.Service.DatabaseService.IRPMSDatabaseService)(tempBinding, tempAddress)
            With tempFactory
                tempProxy = .CreateChannel()
            End With
            'Setting the contracts to the channel
            With tempProxy
                HandoverDataset = .GetHandover(UserName, Password)

            End With
            Return HandoverDataset
        Catch ex As Exception
            Throw ex
        End Try
    End Function

I call the proxy which in turns access the service and it is as follows:

 Private Sub frmHandover_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    'Load the dataset and bind the controls.
    Try
        HandoverData = New DatabaseProxyClass.ECDBDatabase.Service.DatabaseProxy

        User = frmEhawkRPMS.UserN
        Pass = frmEhawkRPMS.PassW
        HandoverSet = New DataSet
        HandoverSet = HandoverData.GetHandover(User, Pass)

My problem starts when I attempt to access the service from the proxy I get the following error: "Data Error: The server was unable to process the request due to an internal error.
For more information about the error, either turn on IncludeExceptionDetailInFaults(either from servicebahviorattribute or from the configuration behaviour) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft.net Framework 3.0 SDk documentation and inspect the server trace logs."
I've attemtped to enable the exceptions but get an error that says there is already a service behavior and it doesn't allow me to add the exceptiondetailfaults to the service behavior and there are not too many if any examples for adding this in a self hosted WCF with VB.
I'm looking for some advice on adding the exception in VB or if someone notice what my problem is to begin with and can point it out that would be greatly appreciated as well.
Also here is my app config that is in my console application that starts the service.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.web>
<compilation debug="true" />
</system.web>
 <system.serviceModel>
   <services>
     <service name="ECDBService.ECDBDatabase.Service.RPMSDatabaseService">
       <endpoint address="net.tcp://localhost:9010/ECDBDatabase.Service"
              binding="netTcpBinding"
              contract="ECDBService.ECDBDatabase.Service.IRPMSDatabaseService" />
        <endpoint address ="" binding="wsHttpBinding"         contract="ECDBService.ECDBDatabase.Service.RPMSDatabaseService">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <!-- Metadata Endpoints -->
    <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. -->
    <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
    </services>     
  </system.serviceModel>
 </configuration>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文