我正在尝试将Excel连接到WCF服务,但是我似乎甚至无法遇到一个琐碎的情况...当我尝试在Excel中创建代理时,我会遇到无效的语法错误。我已经将Visual Studio调试器附加到Excel,并确定真正的错误是“未找到接口”。我知道该服务有效,因为Visual Studio创建的测试客户端还可以...因此问题在VBA Moniker字符串中。

我希望找到两件事之一:

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/"""
有帮助吗?

解决方案 2

我能够通过在绰号字符串上的某些关键位置添加一些引号来解决此问题……不幸的是,将调试器附加到Excel会给您带来的反馈,因此修复它是猜测和检查的练习。在VBA中,您需要在绰号中的每个字符串周围的双引号(“”)。此外,即使我将合同中的地址指定为较低的案件,MEX地址中的“ MEX”一词也必须大写。去搞清楚。

其他提示

我将创建一个客户端作为.NET对象,将其注册为com对象,然后通过com从vba访问它

更新:我找到了这篇文章:http://msdn.microsoft.com/en-us/library/ms752245.aspx

似乎要使用绰号,您必须生成客户端,然后将其注册,然后使用其GUID作为您的类型,而不是类型的类型

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top