为什么出现设计时间错误,提示“找不到默认端点元素”?

时间:2019-06-19 20:10:48

标签: c# .net wcf-binding class-library wcf-endpoint

我们有一个Windows Form,C#、. NET 4.5.1应用程序,该应用程序也在C#和4.5.1中调用类库。它已经工作了好多年没有问题。本周,当我尝试进行更改时,遇到了一个奇怪的错误和一个奇怪的解决方法。我很想知道如果有人可以解释这是怎么回事。

所以我得到的错误是在主窗体上。错误是“在ServiceModel客户端确认部分中找不到引用合同'ShipService.IShipService'的默认终结点元素。这可能是因为在您的应用程序中找不到配置文件,或者是因为在客户元素。”

我找到了多个链接(请参阅hereherehere),以确保类库中的配置与主程序中的配置匹配。我尝试了这一点,但仍然出现错误。除了“检查您的配置”外,我没有发现其他建议。

在这里变得奇怪。如果我删除本地副本并从master分支下载代码,则只要不打开MainForm.cs,我就可以没有错误地运行该程序。因此,我对其进行了调试,发现该方法中的错误位于业务逻辑层中:

Run As...

如果我注释掉了上面的内容,而是创建了ShipServiceClient的新实例而不是调用ResetShipService,则一切正常。该程序运行,我可以打开MainForm.cs。我只是将其移到同一班的其他地方。我不知道为什么这样做或这里发生了什么。除了注释掉代码并将其移动到其他地方之外,有人可以解释正在发生的事情和/或为我提供更好的解决方法吗?

这是其中涉及的内容:

<?php
$server = 'localhost';
$user = 'root';
$pass = '';
$db = 'sqli';
$connection = new mysqli($server, $user, $pass, $db);
if ($connection->connect_error) {
    die("Connection error!");
}
$sql = "SELECT * FROM sqli_users WHERE username='".$_POST["username"]."' AND password'".$_POST["password"]."'";
$result = $connection->query($sql);
if($result == false)
    ?><script>alert('Wrong password')</script><?php
while ($result != false) {
    ?><script>alert('Right password')</script><?php
}
$connection->close();

?>
<!DOCTYPE HTML>

<html>
    <head>
        <title>Admin login</title>
    </head>
    <body>
        <link rel="icon" href="favicon.png">
        <h2>Admin login</h2>
        <form method="post" action="c2.php">
            Username:<br/>
            <input type="text" name="username"><br/>
            Password:<br/>
            <input type="password" name="password"><br/>
            <br/>
            <input type="submit" value="Log in">
        </form>
    </body>
</html>

这是我遇到的错误: enter image description here

以下是错误来自的BLL中的代码:

public void ResetShipService()
{
    shipClient = new ShipServiceClient();
}

这是ItemsControl.cs中创建BLL实例的代码:

ShippingService - class library
- Endpoint is net.tcp://localhost:9000/Shipping
- I have no access to change this class library

ShippingApplication - Windows Form Application
- Shipping - Main project
    - MainForm.cs - This is the form that shows the design time error
    - User controls
        - ItemsControl.cs - This is a user control in MainForm.cs that calls the business logic layer
- ShippingBusinessLogicLayer - Business logic layer
    - ShippingBLL.cs - Calls the ShippingService class library

这是MainForm.cs设计器中的代码,用于设置用户控件:

public class ShipmentBLL
{
    private ShipServiceClient shipClient;

    public ShipmentBLL()
    {
        ResetShipService();
    }

    public void ResetShipService()
    {
        shipClient = new ShipServiceClient(); // If I comment out this, the design time error goes away
    }

    private ShipmentDTO processShipment (QuickShipmentDTO QuickShipmentDTO, bool TestShipment = false, bool RelieveInventory = true)
    {
        Ship ship = new Ship();

        if (shipClient.InnerChannel.State == System.ServiceModel.CommunicationState.Faulted)
        {
            ResetShipService(); 
        }

        ship = shipClient.ProcessShipment(ship, TestShipment);
    }
}

此处是ShippingService中app.config的服务模型部分:

public partial class ItemsToShipControl : UserControl
{
    public ItemsToShipControl()
    {
        InitializeComponent();

        shipmentBLL = new ShipmentBLL();
    }
}

这是Shipping项目中的app.config:

this.ItemsToShipPane = new NTSupply.NTShipping.UserControls.ItemsControl();
this.Controls.Add(this.ItemsToShipPane);
private UserControls.ItemsControl ItemsToShipPane;

这是BLL中的app.config:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
            <binding name="longTimeoutBinding" receiveTimeout="00:30:00" sendTimeout="00:30:00">
            </binding>
        </netTcpBinding>
    </bindings>
    <services>
       <service behaviorConfiguration="ShippingServiceBehavior" name="NTSupply.Shipping.ShipService">
           <endpoint address="net.tcp://localhost:9000/Shipping" binding="netTcpBinding" bindingConfiguration="longTimeoutBinding"
      contract="NTSupply.Shipping.IShipService" />
           <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
           <host>
               <baseAddresses>
                  <add baseAddress="net.tcp://localhost:9000/Shipping" />
               </baseAddresses>
           </host>
      </service>
    </services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ShippingServiceBehavior">
      <serviceMetadata/>
      <serviceDebug includeExceptionDetailInFaults="True" />
      <dataContractSerializer maxItemsInObjectGraph="65536" />
    </behavior>
  </serviceBehaviors>
    </behaviors>
</system.serviceModel>

1 个答案:

答案 0 :(得分:0)

默认情况下,当我们通过在类库项目中添加服务引用来调用服务时,在与主项目合作期间可能会出现一些问题。服务代理仅使用主项目配置( Appconfig ),而不使用在类库项目中自动生成的先前配置。为了使其正常工作,我们必须将类库项目中的ServiceModel部分移到托管环境可以识别的配置文件中,即主项目配置文件。
https://docs.microsoft.com/en-us/dotnet/framework/wcf/deploying-a-wcf-library-project
在您的实际方案中,客户端代理实例的过程将尝试查找客户端端点,相关的绑定属性等。这可能会带来麻烦。我们可以使用端点的名称来实例化客户端代理,前提是该配置可以被主项目识别。

  

NetTcpBinding_IShipService

 ServiceReference1.Service1Client client = new  ServiceReference1.Service1Client("NetTcpBinding_IShipService");

请随时告诉我是否有什么可以帮忙的。