0

我是 .net 和 WCF 的新手。我正在尝试构建一个 WCF 服务原型,该原型将从数据库表中获取数据,并根据数据使用 SMTP 交换发送电子邮件。
对于原型,我创建了一个接口和服务,并在 IIS 5.1 中托管了 WCF 服务。对于我的测试控制台,我已经引用了这个服务。现在,当我运行我的应用程序时,我从控制台收到错误消息。

这是代码的样子:

namespace MyWcfConsoleApp
{
class Program
{
    static void Main(string[] args)
    {
        // Create a proxy object and connect to the service
        EmailServiceClient proxy = new EmailServiceClient();

        // Test the operations in the service
        Console.WriteLine("Test1");
        //BulkEmailDTOList EmailInfo = proxy.GetBulkEmailInfo(1);

        EmailService.IEmailService EmailInfo = ServiceLocator.Resolve<IEmailService>();
        BulkEmailDTOList result = new BulkEmailDTOList();
        result = EmailInfo.GetBulkEmailInfo(1);

这就是我在控制台应用程序中的 App.Config 中的内容:

    <configuration>
    <configSections>
    <section name="Dependency" type="WW.EnterpriseLibrary.Dependency.ServiceLocatorConfigurationSection, WW.EnterpriseLibrary"/>
  </configSections>
  <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IEmailService" 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="TransportCredentialOnly">
                    <transport clientCredentialType="Windows" proxyCredentialType="None" realm=""/>
                    <message clientCredentialType="UserName" algorithmSuite="Default"/>
                  </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
        <endpoint address="http://localhost/WCFEmailService/BulkEmailService.svc/EmailService" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IEmailService" contract="EmailService.IEmailService" name="BasicHttpBinding_IEmailService"/>
        </client>
    </system.serviceModel>
  <startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
  <Dependency>
    <dependencies>
      <add name="IEmailService" type="C:\BulkEmailPrototype\TestWcfEmailService\TestWcfEmailServiceLibrary.WW.Common.Service.Impl.EmailService, C:\BulkEmailPrototype\TestWcfEmailService\TestWcfEmailServiceLibrary.WW.Common.Service.Impl" />
    </dependencies>
  </Dependency>
</configuration>

请让我知道我做错了什么。有什么简单的方法可以调用 DTO 来检索我的数据吗?正如我所说,我对此很陌生,所以度过了艰难的时光..

这是我的实现:

using BulkEmailDac = WW.Common.DataAccess.Impl.BulkEmailDac;

namespace WW.Common.Service.Impl
{
public class EmailService : IEmailService
{

    private BulkEmailDac emailDac;
    ErrorMsg err_msg = new ErrorMsg();


    public EmailService()
    {
        emailDac = new BulkEmailDac();
    }

                 public BulkEmailDTOList GetBulkEmailInfo(int recordLimit)
         {

             try
             {

                 return emailDac.GetBulkEmailInfo(recordLimit);
             }
             catch (Exception ex)
             {
                 if (ex is FaultException<OperationFault>)
                 {
                     throw;
                 }
                 else
                 {

                     //LOG AND THROW AN UNKNOWN EXCEPTION
                     ApplicationLog.WriteError(DateTime.Now.ToString() + "|"
                                               + "GetBulkEmailInfo" + "|"
                                               + ex.Message + "|"
                                               + ex.StackTrace);
                     throw new FaultException<OperationFault>(new OperationFault(ex.Message, ErrorMessages.Unknown_Error_Code));
                 }
             }

这是合同:

namespace WW.Common.Service.Contract
{
   [ServiceContract]
    public interface IEmailService
    {

     [OperationContract]
     double Add(double n1, double n2);

     [OperationContract]
     double Subtract(double n1, double n2);

     [OperationContract]
     double Multiply(double n1, double n2);

     [OperationContract]
     double Divide(double n1, double n2);

     [OperationContract]
     [FaultContractAttribute(typeof(OperationFault))]
     BulkEmailDTOList GetBulkEmailInfo(int recordLimit);

     [OperationContract]
     string Abc(string s1);

    }

这是我的服务中的 Web.Config 文件:

 <configuration>
  <configSections>
    <section name="Dependency" type="WW.EnterpriseLibrary.Dependency.ServiceLocatorConfigurationSection, WW.EnterpriseLibrary"/>
  </configSections>
  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning, ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
      <source name="System.ServiceModel" switchValue="Warning,ActivityTracing"
        propagateActivity="true">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelTraceListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="c:\bulkemailprototype\testwcfemailservice\wcfemailservice\web_messages.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
      <add initializeData="c:\bulkemailprototype\testwcfemailservice\wcfemailservice\web_tracelog.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelTraceListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
  </system.diagnostics>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <connectionStrings>
    <add name="WWDbConnect"
         connectionString="Data Source=(labdev0320);USER ID = wwsa; Password = trex777sa;Max Pool Size=200;"
         providerName="System.Data.OracleClient" />
  </connectionStrings>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBindingWithNoSecurity" maxBufferPoolSize="524288" maxReceivedMessageSize="500000">
          <security mode="Transport">
            <transport clientCredentialType="Certificate" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client/>
    <services>
        <service name="WW.Common.Service.Impl.EmailService" behaviorConfiguration="EmailService">
          <host>
            <baseAddresses>
              <add baseAddress="http://localhost:8270/Design_Time_Addresses/TestWcfEmailServiceLibrary/EmailService/"   />
            </baseAddresses>
          </host>
          <endpoint address="EmailService" binding="basicHttpBinding" contract="WW.Common.Service.Contract.IEmailService" />
          <endpoint address="mex" binding="basicHttpBinding"
                    name="mexEndpoint" contract="IMetadataExchange" />
        </service>
      </services>
      <behaviors>
      <serviceBehaviors>
          <behavior name="EmailService">
            <serviceDebug httpHelpPageEnabled="true" includeExceptionDetailInFaults="true"/>
            <serviceMetadata httpGetEnabled="true" />
            <serviceSecurityAudit auditLogLocation="Application"
               suppressAuditFailure="true"
               serviceAuthorizationAuditLevel="Success"
               messageAuthenticationAuditLevel="Success" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="false"
        logMessagesAtServiceLevel="true" logMessagesAtTransportLevel="true"
        maxMessagesToLog="3000" />
    </diagnostics>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <Dependency>
    <dependencies>
      <add name="IEmailService" type="WW.Common.Service.Impl.EmailService, WW.Common.Service.Impl" />
    </dependencies>
  </Dependency>
</configuration>
4

1 回答 1

0

首先,您的端点看起来不正确。

在服务的 Web 配置中, in 的值是relativeAddress什么<serviceActivations>?这是您应该从客户端应用程序中找到的真正路径。

此外,测试 WCF 服务的一种快速而肮脏的方法是使用位于 VISUAL_STUDIO_INSTALL_PATH\Common7\IDE 中的 WCFTestClient。一旦你知道你真正的端点,你就可以从这个应用程序中测试它。

于 2013-02-15T22:18:50.933 回答