How to solve the interface with another program in other solutions?

A: The release interface code written in service to the public network, other projects use the "Add Service Reference", add the service to publish their own projects.

 

URL referenced in the learning process:

https://www.cnblogs.com/peterpc/p/4628441.html

https://www.cnblogs.com/Brambling/p/6815565.html

https://www.cnblogs.com/pangguoming/p/7494946.html

https://blog.csdn.net/friendan/article/details/49362409

 

Problems encountered in the process:

Question 1: how Publishing Service

1) Create a new service

 

2) Service to write an interface, the interface above, remember to add a [WebMethod].

 

3) release

 

Second problem: how to call service

1) In the file you need to call the service, right-click the Add Service Reference [], the specific process see above URL. I stepped pit: in the UI layer is added in reference to how it can not be called DAL, and found references to the wrong place.

 

 

2) After successfully added, these files will be more

 

 

 

 

3) references in the code, the specific code to see how your interface written. References are the same way: your name + service interface.

Code:

WebReference.WxSentMsg ws = new WebReference.WxSentMsg (); - the name of the service namespace.

WebReference.SentMsg sm = new WebReference.SentMsg (); - the name of the service interface parameters.

sm.cntr_no = dr [ "CNTR_NO"] ToString (); -. Fill parameter passing

sm.cntr_request = dr["REQUEST_TYPE"].ToString();

sm.cntr_size = dr["CNTR_SIZE_CODE"].ToString();

sm.pk_id = dr["PK_ID"].ToString();

sm.truck_no = dr["TRUCK_NO"].ToString();

sm.user_name = userCode;

ws.WxMsgPush(sm); --传参

 

笔者在两个项目中都引用这个服务,但方法不一样。以下是在winform项目中添加:

 

同样,

1.添加服务引用,生成的文件如下。为什么和上面生成的不一样,= =我也不知道。可能是项目不一样,一个是winform项目,一个是只写服务的项目。

 

2.调用服务。这个步骤笔者踩过几个坑。

跟上面的一样的方法写好代码,本以为能过的,结果在第一行就出现问题:

---------------------------------------------------------------

检测到 BindingFailure

Message: 显示名为“CTMS.DAL.XmlSerializers”的程序集未能加载到 ID 为 1 的 AppDomain 的“Load”绑定

上下文中。错误的原因为: System.IO.FileNotFoundException: 未能加载文件或程序

集“CTMS.DAL.XmlSerializers”或它的某一个依赖项。系统找不到指定的文件。

---------------------------------------------------------------

 

 

重新引用几次都不行,原本我的引用方式是:高级-添加Web引用,

 

尝试几次后,换了方式:直接输入地址引用,没注意两种方法生成的文件有没有不一样。

 

网上找了很多论坛,发现是调用的方法出现问题,具体网址最上面已经复制出来了。

 

接着我改调用方法,但输入服务名,点出来的东西,前面一截是我的,后面的为什么都加了Response、ResponseBody啊! = =

 

 

我只能按着感觉取一个试试,然后用又出现下面问题:

 

---------------------------------------------------------------

发生了 System.InvalidOperationException

Message=在 ServiceModel 客户端配置部分中,找不到引用协定“ServiceReference1.WxSentMsgSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素中找不到与此协定匹配的终结点元素。

---------------------------------------------------------------

 

 

最后,终于找对了:

 

我服务里的命名空间是WxSentMsg,但这里的调用要用:WxSentMsgSoapClient,这样就过了……

 

以上,在两个项目中引用web服务我总算是做完了。挺有成就感的,毕竟在此之前我什么都不懂,都是靠网上搜索,自己摸索出来的。这就是做程序员的乐趣吧,像搭积木一样,用逻辑一点点寻找线索、整合,一步步攻克难关,把自己想要的模型建成,实现后挺有成就感的。享受这个过程。

 

 

Guess you like

Origin www.cnblogs.com/aitongmaomao/p/10930443.html