从事件接收器调用Web服务

时间:2011-02-21 11:41:30

标签: c# webservice-client

我正在使用C#构建一个事件接收器来监视特定的Exchange服务器收件箱。 我的实现基于以下示例:http://www.codeproject.com/KB/exchange/ManagedEventSinks.aspx?fid=382114&df=90&mpp=25&noise=3&sort=Position&view=Quick&fr=26#xx0xx

我尝试将一些特定信息发送到Web服务,而不是示例中描述的功能。 我使用visual studio将服务引用添加到我的类库和以下代码中以调用Web服务中的单个方法:

public void OnSave(IExStoreEventInfo pEventInfo, string bstrURLItem, int lFlags)
{
    try
    {
        Message iMessage = new MessageClass();
        iMessage.DataSource.Open(bstrURLItem, null,
               ADODB.ConnectModeEnum.adModeRead,
               ADODB.RecordCreateOptionsEnum.adFailIfNotExists,
               ADODB.RecordOpenOptionsEnum.adOpenSource, "", "");

        string sub= iMessage.Subject;

        string body = iMessage.HTMLBody;

        MyWSSoapClient wsc = new MyWSSoapClient();
        wsc.SingleMethodinWS(sub, body); 
    }
    catch (Exception ex)
    {
    }
}

在构建COM组件并将事件接收器添加到收件箱并测试后,我收到错误:

Could not find default endpoint element that references contract 'MyWS.MyWSSoap' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
   at System.ServiceModel.Description.ConfigLoader.LoadChannelBehaviors(ServiceEndpoint serviceEndpoint, String configurationName)
   at System.ServiceModel.ChannelFactory.ApplyConfiguration(String configurationName)
   at System.ServiceModel.ChannelFactory.InitializeEndpoint(String configurationName, EndpointAddress address)
   at System.ServiceModel.ChannelFactory`1..ctor(String endpointConfigurationName, EndpointAddress remoteAddress)
   at System.ServiceModel.EndpointTrait`1.CreateSimplexFactory()
   at System.ServiceModel.EndpointTrait`1.CreateChannelFactory()
   at System.ServiceModel.ClientBase`1.CreateChannelFactoryRef(EndpointTrait`1 endpointTrait)
   at System.ServiceModel.ClientBase`1.InitializeChannelFactoryRef()
   at System.ServiceModel.ClientBase`1..ctor()
   at ReplyParserEventSink.AnswerQuestionWS.AnswerQuestionWSSoapClient..ctor()
   at ReplyParserEventSink.AsyncParser.OnSave(IExStoreEventInfo pEventInfo, String bstrURLItem, Int32 lFlags)

编辑: 这是我在app config

中找到的
<system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="MyWSSoap" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
<endpoint address="http://127.0.0.1/MyProject/WebService/MyWS.asmx"
                binding="basicHttpBinding" bindingConfiguration="MyWSSoap"
                contract="MyWS.MyWSSoap" name="MyWSSoap" />
</system.serviceModel>

@Alexander:我试过你说的但是我得到了同样的错误。感谢

2 个答案:

答案 0 :(得分:1)

这当然是VS2010。事情是使用app.config中的自动生成代码。

检查app.config。此文件包含绑定和端点的所有防御。只需检查端点1,检查是否描述了您的端点,并定义了绑定。

之后尝试:

MyWSSoapClient wsc = new MyWSSoapClient( "<endpointConfigurationName>" );

看看会发生什么。

同样在<client>区域,您将找到端点的描述。尝试手动更改绑定到basicHttpBinding - 这对我有用。

    <endpoint address="http://service.address.com"
        binding="basicHttpBinding" bindingConfiguration="yourService"
        contract="Domain.Serice" name="serviceName" />

答案 1 :(得分:0)

我正在回答我自己的问题,因为我找到并修复了问题

问题是来自类库的app.config文件的信息没有进入com组件,因此错误。

我通过在以下示例中设置我的代码中的所有参数值来修复它:Consume a SOAP web service without relying on the app.config

感谢大家的建议。