首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何调用IsOneWay WCF服务并正确关闭客户端

如何调用IsOneWay WCF服务并正确关闭客户端
EN

Stack Overflow用户
提问于 2012-11-13 23:50:07
回答 1查看 913关注 0票数 0

我是非常新的WCF,我已经建立了一个测试网站和服务。我试着做两件事:

  1. 调用服务而不阻塞用户
  2. 正确关闭客户端

我创建了一个webservice,并为它提供了isoneway属性。我成功地调用了它,没有阻塞用户,但我担心我不会关闭客户端。如何在不阻塞用户并且仍然正确关闭客户端的情况下调用此服务?我应该使用异步方法(TestServiceAsync)吗?我应该使用BeginXX,EndXX方法吗?

客户端:

代码语言:javascript
复制
Dim callservice As New WCFEmailServices.EmailServiceClient()
callservice.TestService()
callservice.Close()

Webservice:

代码语言:javascript
复制
    <ServiceContract()> _
    Public Interface IEmailService


        <OperationContract(IsOneWay:=True)> _
        Sub TestService()


    End Interface


    Public Class EmailService
    Implements IEmailService

    Public Sub TestService() Implements IEmailService.TestService

        Dim srvBody As String = ""
        srvBody = "START: " + DateTime.Now.ToLongTimeString() + System.Environment.NewLine
        Thread.Sleep(10000)
        srvBody += "END: " + DateTime.Now.ToLongTimeString() + System.Environment.NewLine

        Me.SendEmail("test@gmail.com", "test", srvBody, Nothing)

    End Sub


    Function SendEmail(ByVal srpTo As String, ByVal srpSubject As String, ByVal srpBody As String, ByVal srpAttachmentPath As String) As Boolean

        Dim MailMsg As New MailMessage(New MailAddress("No_Reply@test.com"), New MailAddress(srpTo))
        MailMsg.BodyEncoding = Encoding.UTF8
        MailMsg.Subject = srpSubject
        MailMsg.Body = srpBody
        MailMsg.IsBodyHtml = True

        If srpAttachmentPath IsNot Nothing Then
            Dim srvAttachment As New Attachment(srpAttachmentPath)
            MailMsg.Attachments.Add(srvAttachment)
        End If


        Dim SmtpMail As New SmtpClient("smtp.gmail.com", 587)
        SmtpMail.UseDefaultCredentials = False
        SmtpMail.EnableSsl = True
        SmtpMail.Credentials = New System.Net.NetworkCredential("No_Reply@test", "password")

        Try
            SmtpMail.Send(MailMsg)
        Catch
            Return False
        End Try
        Return True
    End Function

End Class

WebConfig:

代码语言:javascript
复制
            <system.serviceModel>
        <bindings>
   <wsHttpBinding>
    <binding name="WSHttpBinding_IEmailService" closeTimeout="00:01:00"
     openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
     bypassProxyOnLocal="false" transactionFlow="false" hostNameComparisonMode="StrongWildcard"
     maxBufferPoolSize="524288" maxReceivedMessageSize="65536" messageEncoding="Text"
     textEncoding="utf-8" useDefaultWebProxy="true" allowCookies="false">
     <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
      maxBytesPerRead="4096" maxNameTableCharCount="16384" />
     <reliableSession ordered="true" inactivityTimeout="00:10:00"
      enabled="false" />
     <security mode="Message">
      <transport clientCredentialType="Windows" proxyCredentialType="None"
       realm="" />
      <message clientCredentialType="Windows" negotiateServiceCredential="true"
       algorithmSuite="Default" establishSecurityContext="true" />
     </security>
    </binding>
   </wsHttpBinding>
  </bindings>
  <client>
   <endpoint address="http://localhost:61450/EmailService.svc" binding="wsHttpBinding"
    bindingConfiguration="WSHttpBinding_IEmailService" contract="WCFEmailServices.IEmailService"
    name="WSHttpBinding_IEmailService">
    <identity>
     <servicePrincipalName value="host/localhost" />
    </identity>
   </endpoint>
  </client>
  <services>
   <service behaviorConfiguration="WCFService.Service1Behavior"
    name="WCFService.Service1">
    <endpoint address="" binding="wsHttpBinding" contract="WCFService.IService1">
     <identity>
         <servicePrincipalName value="host/localhost" />
     </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
   </service>
   <service behaviorConfiguration="ClientWebApp.EmailServiceBehavior"
    name="ClientWebApp.EmailService">
    <endpoint address="" binding="wsHttpBinding" contract="ClientWebApp.IEmailService">
     <identity>
         <servicePrincipalName value="host/localhost" />
     </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
   </service>
  </services>
        <behaviors>
   <serviceBehaviors>
    <behavior name="WCFService.Service1Behavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
    <behavior name="ClientWebApp.EmailServiceBehavior">
     <serviceMetadata httpGetEnabled="true" />
     <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>
   </serviceBehaviors>
  </behaviors>
    </system.serviceModel>
EN

回答 1

Stack Overflow用户

发布于 2012-11-14 02:06:31

在IsOneWay调用之后,您将正确关闭客户端。基于绑定,close()一直处于阻塞状态,直到IsOneWay完成。

您可以在这里找到更多信息:单向WCF调用后在服务代理块上调用Close()

在这个博客中,答案是:WCF最佳做法#5

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13370896

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档