我正在尝试将 excel 连接到 WCF 服务,但我似乎无法让一个微不足道的案例工作......当我尝试在 excel 中创建代理时出现无效的语法错误。我已将 Visual Studio 调试器附加到 Excel,并发现真正的错误是“找不到接口”。我知道该服务有效,因为由 Visual Studio 创建的测试客户端没问题……所以问题出在 VBA 名字对象字符串中。
我希望找到两件事之一:
1)对我的名字字符串进行更正以使这项工作正常进行,或者
2) 要下载的现有示例项目,该项目具有有效的主机和客户端的源代码。
这是我的 VBA 客户端的代码:
Dim addr As String
addr = "service:mexAddress=net.tcp://localhost:7891/Test/WcfService1/Service1/mex, "
addr = addr + "address=net.tcp://localhost:7891/Test/WcfService1/Service1/, "
addr = addr + "contract=IService1, contractNamespace=http://tempuri.org, "
addr = addr + "binding=NetTcpBinding_IService1, bindingNamespace=""http://tempuri.org"""
MsgBox (addr)
Dim service1 As Object
Set service1 = GetObject(addr)
MsgBox service1.Test(12)
我有以下服务:
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
它有以下配置文件:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service behaviorConfiguration="WcfService1.Service1Behavior"
name="WcfService1.Service1">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="WcfService1.IService1">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:7891/Test/WcfService1/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="WcfService1.Service1Behavior">
<serviceMetadata httpGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
编辑:
对我有用的更新绰号如下
Dim addr As String
addr = "service:mexAddress=""net.tcp://localhost:7891/Test/WcfService1/Service1/Mex"", "
addr = addr + "address=""net.tcp://localhost:7891/Test/WcfService1/Service1/"", "
addr = addr + "contract=""IService1"", contractNamespace=""http://tempuri.org/"", "
addr = addr + "binding=""NetTcpBinding_IService1"", bindingNamespace=""http://tempuri.org/"""