可能有些愚蠢而显而易见,但只是从WCF服务和VBA开始并试图学习。
因此,我有一个非常基本的WCF服务应用程序,该应用程序具有将字节数组作为输入并返回布尔值的方法。字节数组包含在文档的内容中,并在VBA中生成。我想做的是将自己从VBA连接到WCFService,并传递字节数组,然后将结果返回到vba项目中。问题是我不确定该怎么做。
这是我尝试的方法: VBA代码
Public Function GetServiceData() As XMLHTTP
Dim httpData As New XMLHTTP
Dim webServiceURL As String
webServiceURL = ipAddress & port & "/TestService.svc/AttachFile"
httpData.Open "POST", webServiceURL, False
httpData.setRequestHeader "Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"
httpData.send Example
Set GetServiceData = httpData
End Function
End FunctionPublic Function GetFileBytes(ByVal path As String) As Byte()
Dim lngFileNum As Long
Dim bytRtnVal() As Byte
lngFileNum = FreeFile
If LenB(Dir(path)) Then ''// Does file exist?
Open path For Binary Access Read As lngFileNum
ReDim bytRtnVal(LOF(lngFileNum) - 1&) As Byte
Get lngFileNum, , bytRtnVal
Close lngFileNum
Else
Err.Raise 53
End If
GetFileBytes = bytRtnVal
Erase bytRtnVal
End Function
Public Function Example() As Byte()
Dim byteFile() As Byte
byteFile = GetFileBytes("C:\Desktop\FilesForTesting\TestDocument13_2.docx")
Example = byteFile
End Function
Wcf服务:接口
[ServiceContract]
public interface IAttachItService
{
[OperationContract]
[WebInvoke]
(Method = "POST",
UriTemplate = "/AttachFile/{fileName}",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
string AttachFile(byte [] fileName);
}
Wcf服务: Web配置
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.5.2" />
<httpRuntime targetFramework="4.5.2"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors >
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="REST">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="BestService.AttachItService" behaviorConfiguration="ServiceBehavior">
<endpoint binding="webHttpBinding" contract="BestService.IAttachItService" behaviorConfiguration="REST"/>
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>
运行vba宏时,找不到服务方法,因此出现错误404 Not found。 唯一可行的方法是在URI的末尾添加参数,这不是解决方案,因为il收到的错误URI太长。
谢谢。