azurewebsites.net 上发布的 WCF 服务没有 return sql 记录
Published WCF service on azurewebsites.net does not return sql record
我正在尝试使用 visual basic 为 iPhone 应用程序创建 API。为此,我编写了一个 WCF Web 服务,它可以在本地主机上完美运行。
正在调用:getTimeSheetRecord/{deviceID} returns
上图显示代码能够连接到 SQL 数据库和 return 记录。
由于我需要能够从公司网络外部访问 API,因此我需要托管它以便可以从任何地方访问。因此,我在 Visual Studio 2015 年使用 Build -> Publish 创建了 Microsoft Azure App Server。一切顺利,直到我 运行 我的函数 return 一条数据库记录。它没有给我与本地主机相同的结果,事实上,我认为它甚至没有连接到数据库。请看下图:
上图是我正在尝试开始工作的已发布 WCF。这不是 return 上图中显示的记录。代码没有任何不同,因为我所做的只是在 visual studio 2015 年发布它。
我的 WCF 连接到 Azure VM 中 运行 的 MS SQL 服务器。
这是我的 WCF 中用于连接到数据库的代码:
Imports System.Configuration
Imports System.Data.SqlClient
Module modGlobal
'Global Database Objects
Public uDBase As ADODB.Connection
Public uCommand As ADODB.Command
Public bConnection As Boolean
Public bActive As Boolean = True
Public sErrDescription As String
Public Sub OpenConnection()
'** Open Database Connection
' Error Checking
On Error GoTo Err_OpenConnection
' Open Database Connection
uDBase = New ADODB.Connection
uDBase.ConnectionString = "Provider=SQLOLEDB;Initial Catalog=BMSSaltire;Data Source=tcp:xxxxxxxx.cloudapp.net,xxxxx;User ID=xxxxxUser;Password=xxxxx"
uDBase.CommandTimeout = 0
uDBase.Open()
Err_OpenConnection:
If Err.Number <> 0 Then
CloseConnection()
If
End Sub
Sub CloseConnection()
'** Close Database Connection
uDBase = Nothing
uCommand = Nothing
End Sub
End Module
这是我的 Service.svc.vb
Public Function GetTimeSheetRecord(ByVal deviceID As String) As List(Of wsTimeSheet) Implements ServiceInterface.GetTimeSheetRecord
Dim results As List(Of wsTimeSheet) = New List(Of wsTimeSheet)
Try
Dim uRecSnap As ADODB.Recordset
If uDBase Is Nothing Then
OpenConnection()
bConnection = True
End If
uCommand = New ADODB.Command
With uCommand
.ActiveConnection = uDBase
.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
.CommandTimeout = 0
.Parameters.Append(.CreateParameter("@DeviceID", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 32, deviceID))
.CommandText = "TimeSheet_LoadRecord"
uRecSnap = .Execute
End With
Do Until uRecSnap.EOF
Dim uAction = New wsTimeSheet
uAction.DeviceID = If(IsDBNull(uRecSnap("ActionDate").Value), "", uRecSnap("ActionDate").Value)
results.Add(uAction)
uRecSnap.MoveNext()
Loop
uRecSnap = Nothing
Catch ex As Exception
' Catch Error
If Err.Number <> 0 Then
End If
Finally
' CleanUp
If bConnection Then CloseConnection()
End Try
Return results
End Function
此外,当我尝试发布时,在“设置”中它在“数据库”下显示 "No databases found in the project"。
编辑 2016 年 8 月 3 日
当我将它发布到本地主机 IIS 时,我试图查看它是否有任何不同,但即使那样它也不会 return SQL 数据库表中的记录。当我使用我在设置中 "Web" 下设置的虚拟目录在 Visual Studio 上调试它时,它工作正常。跟Web.config有关系吗?
尝试将连接字符串作为设置放入您的云项目中。
右键单击项目的属性
Select "Settings"
点击"Add Setting"
顺便问一下,您在代码中的连接字符串是否与您在 Azure 门户中看到的相匹配?
希望对您有所帮助。
/弗雷德里克
我的 WCF 正在运行。我必须在我的 Web.config 中做一些小的调整,并更改我的 ADODB 参考 属性 Copy Local = True 和 Embed Interop Type = False。
这是我的 Web.config 文件。希望有人觉得有用。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<!--<add key="saltireServiceUrl" value="http://[sitename].azurewebsites.net/Service1.svc" />-->
</appSettings>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="JSONWebService.Service1" behaviorConfiguration="JSONWebService.Service1Behaviour">
<endpoint address="" behaviorConfiguration="webBehaviour" binding="webHttpBinding" contract="JSONWebService.ServiceInterface" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="JSONWebService.Service1Behaviour">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://[sitename].azurewebsites.net/"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpErrors errorMode="Detailed" />
<validation validateIntegratedModeConfiguration="false"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
我正在尝试使用 visual basic 为 iPhone 应用程序创建 API。为此,我编写了一个 WCF Web 服务,它可以在本地主机上完美运行。
正在调用:getTimeSheetRecord/{deviceID} returns
上图显示代码能够连接到 SQL 数据库和 return 记录。
由于我需要能够从公司网络外部访问 API,因此我需要托管它以便可以从任何地方访问。因此,我在 Visual Studio 2015 年使用 Build -> Publish 创建了 Microsoft Azure App Server。一切顺利,直到我 运行 我的函数 return 一条数据库记录。它没有给我与本地主机相同的结果,事实上,我认为它甚至没有连接到数据库。请看下图:
上图是我正在尝试开始工作的已发布 WCF。这不是 return 上图中显示的记录。代码没有任何不同,因为我所做的只是在 visual studio 2015 年发布它。
我的 WCF 连接到 Azure VM 中 运行 的 MS SQL 服务器。 这是我的 WCF 中用于连接到数据库的代码:
Imports System.Configuration
Imports System.Data.SqlClient
Module modGlobal
'Global Database Objects
Public uDBase As ADODB.Connection
Public uCommand As ADODB.Command
Public bConnection As Boolean
Public bActive As Boolean = True
Public sErrDescription As String
Public Sub OpenConnection()
'** Open Database Connection
' Error Checking
On Error GoTo Err_OpenConnection
' Open Database Connection
uDBase = New ADODB.Connection
uDBase.ConnectionString = "Provider=SQLOLEDB;Initial Catalog=BMSSaltire;Data Source=tcp:xxxxxxxx.cloudapp.net,xxxxx;User ID=xxxxxUser;Password=xxxxx"
uDBase.CommandTimeout = 0
uDBase.Open()
Err_OpenConnection:
If Err.Number <> 0 Then
CloseConnection()
If
End Sub
Sub CloseConnection()
'** Close Database Connection
uDBase = Nothing
uCommand = Nothing
End Sub
End Module
这是我的 Service.svc.vb
Public Function GetTimeSheetRecord(ByVal deviceID As String) As List(Of wsTimeSheet) Implements ServiceInterface.GetTimeSheetRecord
Dim results As List(Of wsTimeSheet) = New List(Of wsTimeSheet)
Try
Dim uRecSnap As ADODB.Recordset
If uDBase Is Nothing Then
OpenConnection()
bConnection = True
End If
uCommand = New ADODB.Command
With uCommand
.ActiveConnection = uDBase
.CommandType = ADODB.CommandTypeEnum.adCmdStoredProc
.CommandTimeout = 0
.Parameters.Append(.CreateParameter("@DeviceID", ADODB.DataTypeEnum.adVarChar, ADODB.ParameterDirectionEnum.adParamInput, 32, deviceID))
.CommandText = "TimeSheet_LoadRecord"
uRecSnap = .Execute
End With
Do Until uRecSnap.EOF
Dim uAction = New wsTimeSheet
uAction.DeviceID = If(IsDBNull(uRecSnap("ActionDate").Value), "", uRecSnap("ActionDate").Value)
results.Add(uAction)
uRecSnap.MoveNext()
Loop
uRecSnap = Nothing
Catch ex As Exception
' Catch Error
If Err.Number <> 0 Then
End If
Finally
' CleanUp
If bConnection Then CloseConnection()
End Try
Return results
End Function
此外,当我尝试发布时,在“设置”中它在“数据库”下显示 "No databases found in the project"。
编辑 2016 年 8 月 3 日 当我将它发布到本地主机 IIS 时,我试图查看它是否有任何不同,但即使那样它也不会 return SQL 数据库表中的记录。当我使用我在设置中 "Web" 下设置的虚拟目录在 Visual Studio 上调试它时,它工作正常。跟Web.config有关系吗?
尝试将连接字符串作为设置放入您的云项目中。 右键单击项目的属性 Select "Settings" 点击"Add Setting"
顺便问一下,您在代码中的连接字符串是否与您在 Azure 门户中看到的相匹配?
希望对您有所帮助。 /弗雷德里克
我的 WCF 正在运行。我必须在我的 Web.config 中做一些小的调整,并更改我的 ADODB 参考 属性 Copy Local = True 和 Embed Interop Type = False。
这是我的 Web.config 文件。希望有人觉得有用。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
</configSections>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<!--<add key="saltireServiceUrl" value="http://[sitename].azurewebsites.net/Service1.svc" />-->
</appSettings>
<system.web>
<customErrors mode="Off"/>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" />
</system.web>
<system.serviceModel>
<services>
<service name="JSONWebService.Service1" behaviorConfiguration="JSONWebService.Service1Behaviour">
<endpoint address="" behaviorConfiguration="webBehaviour" binding="webHttpBinding" contract="JSONWebService.ServiceInterface" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="JSONWebService.Service1Behaviour">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
<baseAddressPrefixFilters>
<add prefix="http://[sitename].azurewebsites.net/"/>
</baseAddressPrefixFilters>
</serviceHostingEnvironment>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
<httpErrors errorMode="Detailed" />
<validation validateIntegratedModeConfiguration="false"/>
<!--
To browse web app root directory during debugging, set the value below to true.
Set to false before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers></system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>