当从测试控制台调用时,如何通过DTO从WCF服务获取数据

当从测试控制台调用时,如何通过DTO从WCF服务获取数据,wcf,wcf-data-services,Wcf,Wcf Data Services,我不熟悉.net和WCF。我正在尝试构建一个WCF服务原型,该原型将从数据库表中获取数据,并基于这些数据使用SMTP交换发送电子邮件。 对于原型,我创建了一个接口和服务,并在IIS5.1中托管了WCF服务。对于我的测试控制台,我引用了这个服务。现在,当我运行我的应用程序时,我从控制台收到错误消息 这是代码的样子: namespace MyWcfConsoleApp { class Program { static void Main(string[] args) {

我不熟悉.net和WCF。我正在尝试构建一个WCF服务原型,该原型将从数据库表中获取数据,并基于这些数据使用SMTP交换发送电子邮件。
对于原型,我创建了一个接口和服务,并在IIS5.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);
这是我的服务中的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>

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

在服务的web配置中,
中的
relativeAddress
的值是多少。这是您应该从客户端应用程序访问的真正路径


此外,测试WCF服务的一种快速而肮脏的方法是使用位于VISUAL_STUDIO_INSTALL_PATH\Common7\IDE中的WCFTestClient。一旦您了解了真正的端点,就可以从此应用程序中进行测试。

您在哪里看到服务激活?我在我的应用程序中没有看到它。当我使用调试而不调试运行服务时,即使从WCFTestClient控制台自动出现,在我尝试调用GetBulkEmailInfo时也会出现错误
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);

    }
 <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>