我正在尝试使用Visual Basic为iPhone应用程序创建API。为此,我编写了一个WCF Web服务,它在localhost上运行得很好。
调用:getTimeSheetRecord / {deviceID}返回
上图显示代码能够连接到SQL数据库并返回记录。
由于我需要能够从公司网络外部访问API,因此我需要将其托管以便可以从任何地方访问。因此我使用了Build - >发布以在Visual Studio 2015中创建Microsoft Azure App Server。一切顺利,直到我运行我的函数以返回数据库记录。它没有给我与localhost相同的结果,事实上,我认为它甚至没有与数据库的连接。请参阅下图:
上面的图片是我试图开始工作的已发布的WCF。这不会像上一张图片中显示的那样返回记录。代码没有任何不同,因为我所做的只是通过visual studio 2015发布。
我的WCF连接到Azure VM中运行的MS SQL Server。 这是我的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
当我尝试发布时,在“设置”中,它在“数据库”下面显示“项目中找不到数据库”。
编辑08/03/2016 当我将它发布到localhost IIS时,我试图查看它是否有任何不同,但即便如此,它也不会返回SQL数据库表中的记录。当我在Visual Studio上使用我在“设置”中的“Web”下设置的虚拟目录进行调试时,它工作正常。它与Web.config有什么关系吗?
答案 0 :(得分:0)
尝试将连接字符串作为云项目中的设置。 右键单击项目的属性 选择&#34;设置&#34; 点击&#34;添加设置&#34;
顺便提一下,代码中的连接字符串是否与Azure门户中的连接字符串相匹配?
希望它有所帮助。 /弗雷德里克
答案 1 :(得分:0)
我的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>