简体   繁体   English

这是 WCF 服务应用程序的有效使用吗

[英]Is this a valid use of a WCF Service Application

I have the following scenario:我有以下场景:

  • Program P (can take upto 30 minutes to complete) on box X.方框 X 上的程序 P(最多需要 30 分钟才能完成)。
  • WinForms application on box Y - this gathers the input criteria for P.框 Y 上的 WinForms 应用程序 - 这会收集 P 的输入标准。
  • Because P takes so long I want it to always run on box X and not Y.因为 P 需要很长时间,我希望它总是在盒子 X 而不是 Y 上运行。

I was adviced to try using a WCF Service Application on X. Send a message to X from Y via a service contract and this would then fire program P.有人建议我尝试在 X 上使用 WCF 服务应用程序。通过服务合同从 Y 向 X 发送消息,然后这将触发程序 P。

Is this a valid use of a WCF Service App project?这是对 WCF 服务应用程序项目的有效使用吗?


I have followed these two walk-throughs:我遵循了这两个演练:

  1. Create a WCF service 创建 WCF 服务
  2. Create a console app to consume WCF service 创建一个控制台应用程序来使用 WCF 服务

I now have two projects that seem to talk to each other.我现在有两个项目似乎相互交谈。 I can run the following code from the console app, the method moveData that is in the WCF project successfully updates a database with some information based on the parameters:我可以从控制台应用程序运行以下代码,WCF 项目中的moveData方法根据参数成功地使用一些信息更新了数据库:

        static void Main(string[] args) {
                Service1Client sc = new Service1Client();
                sc.moveData(0,1);
                sc.Close();
        }

I'm very new to this sort of technology - please bear in mind re.我对这种技术很陌生 - 请记住。 the following questions:以下问题:
It only works when I've got the WCF project open or running in Visual Studio - is this as expected?它仅在我打开 WCF 项目或在 Visual Studio 中运行时才有效 - 这是预期的吗? in other words should the consuming app throw an error if the WCF is not running?换句话说,如果 WCF 没有运行,消费应用程序是否应该抛出错误?
ie If I close the instance of Vis Studio with the WCF project and then try running the consuming application I get an error System.ServiceModel.EndpointNotFoundException was unhandled There was no endpoint listening at...followed by address of service How do I let box X make use of this WCF?即如果我使用 WCF 项目关闭 Vis Studio 的实例,然后尝试运行消费应用程序,我会收到错误System.ServiceModel.EndpointNotFoundException was unhandled There was no endpoint listening at...followed by address of service如何让框X利用这个WCF? What do need to install or deploy to that box?什么需要安装或部署到那个盒子?

app.config in the consumer console app looks like the following:消费者控制台应用程序中的 app.config 如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IService1" 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>
        <client>
            <endpoint address="http://....svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IService1" contract="IService1"
                name="BasicHttpBinding_IService1" />
        </client>
    </system.serviceModel>
</configuration>

as per the other suggestions, a windows service is easy to build and setup.根据其他建议,Windows 服务很容易构建和设置。 You can even set it to auto start when the server (BOX X) is started您甚至可以将其设置为在服务器 (BOX X) 启动时自动启动

here is an MSDN article and a Code Project tutorial to get you started.这是一篇MSDN 文章和一个代码项目教程,可帮助您入门。

Essentially:本质上:

public class UserService1 : System.ServiceProcess.ServiceBase  
{

    public UserService1() 
    {
        this.ServiceName = "MyService2";
        this.CanStop = true;
        this.CanPauseAndContinue = true;
        this.AutoLog = true;
    }
    public static void Main()
    {
        System.ServiceProcess.ServiceBase.Run(new UserService1());
    }
    protected override void OnStart(string[] args)
    {
        // Insert code here to define processing.
    }
    protected override void OnStop()
    { 
        // Whatever is required to stop processing
    }
}

EDIT编辑

Then you can persist the data you process on a database or on a file system or wherever, and expose the data over a WCF service, which your client (console app) can then consume.然后,您可以将处理的数据保存在数据库、文件系统或任何地方,并通过 WCF 服务公开数据,然后您的客户端(控制台应用程序)可以使用该服务。

Look at this code for a sample on how you can create this using WCF and a link to the SRC Code as well查看此代码以获取有关如何使用 WCF 创建此代码的示例以及指向 SRC 代码的链接

<system.serviceModel>
 <services>
   <service behaviorConfiguration="returnFaults" name="TestService.Service">
      <endpoint binding="wsHttpBinding" bindingConfiguration=
            "TransportSecurity" contract="TestService.IService"/>
      <endpoint address="mex" binding="mexHttpsBinding" 
            name="MetadataBinding" contract="IMetadataExchange"/>
  </service>
 </services>
 <behaviors>
   <serviceBehaviors>
    <behavior name="returnFaults">
     <serviceDebug includeExceptionDetailInFaults="true"/>
       <serviceMetadata httpsGetEnabled="true"/>
       <serviceTimeouts/>
   </behavior>
  </serviceBehaviors>
 </behaviors>
 <bindings>
    <wsHttpBinding>
       <binding name="TransportSecurity">
             <security mode="Transport">
              <transport clientCredentialType="None"/>
              </security>
        </binding>
      </wsHttpBinding>
 </bindings>
 <diagnostics>
  <messageLogging logEntireMessage="true" 
    maxMessagesToLog="300" logMessagesAtServiceLevel="true" 
    logMalformedMessages="true" logMessagesAtTransportLevel="true"/>
  </diagnostics>
 </system.serviceModel>

//Contract Description
[ServiceContract]
interface IService
{
  [OperationContract]
   string TestCall();
}

//Implementation
public class Service:IService
{
  public string TestCall()
  {
      return "You just called a WCF webservice On SSL
                    (Transport Layer Security)";
  }
}

//Tracing and message logging
<system.diagnostics>
  <sources>
      <source name="System.ServiceModel" 
    switchValue="Information,ActivityTracing" propagateActivity="true">
         <listeners>
           <add name="xml"/>
        </listeners>
      </source>
        <source name="System.ServiceModel.MessageLogging">
        <listeners>
            <add name="xml"/>
         </listeners>
         </source>
    </sources>
        <sharedListeners>
          <add initializeData="C:\Service.svclog" 
        type="System.Diagnostics.XmlWriterTraceListener" name="xml"/>
         </sharedListeners>
       <trace autoflush="true"/>
</system.diagnostics>

Source Code you can Download for this sample to try on your own.. follow the same steps to get your service to work as well This uses SSL by the way WCF Transport Layer Security using wsHttpBinding and SSL您可以下载此示例的源代码以自己尝试.. 遵循相同的步骤使您的服务也能正常工作 这通过使用 wsHttpBinding 和 SSL 的 WCF 传输层安全性方式使用 SSL

I'd probably create a Windows service that watches a database for messages to process stuff.我可能会创建一个 Windows 服务来监视数据库中的消息以处理内容。 The service can then just always be running on your server (which I assume is box X).然后该服务可以始终在您的服务器上运行(我假设是框 X)。 WCF services can start a long running process, but probably should not host it. WCF 服务可以启动一个长时间运行的进程,但可能不应承载它。 Of course I'm assuming a WCF service hosted by Asp.Net.当然,我假设有一个由 Asp.Net 托管的 WCF 服务。 You can build a Windows service that hosts WCF as well, which would eliminate the need for the service to monitor a message database.您也可以构建承载 WCF 的 Windows 服务,这将消除该服务监视消息数据库的需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM