1. Environment setting
Working directory:
D:\workspace\ (catalogue is free)
Required Applications:
SvcUtil.exe (the tool that parses the URL of WCF to generate CS and config files) Source: The vs.net PC's c drive is searched and copied into the D:\workspace\.
Csc.exe (tool for generating DLL via CS file) Source: C-Drive search out, not copy.
Required DLLs:
System.ServiceModel.dll Source: The framework of the PC in the C-disk search out (the version number to the SvcUtil.exe generated by the CS file required), copied to D:\workspace\.
2. Command line
(Bold: command line; bold Italic: replaceable WCF parameter; Scarlet Letter: Description; Other: console output)
#跳转到工作目录
PS c:\users> CD D:\workspace\
#解析wcf的url生成cs和config文件
PS d:\workspace> . \svcutil.exe https://test.xxxxxxxxxxx.com/API_v1/WCFService.svc
Microsoft (R) Service Model Metadata Tool
[Microsoft (R) Windows (r) Communication Foundation, Version 3.0.4506.648]
Copyright (c) Microsoft Corporation. All rights reserved.
Attempting to download metadata from ' https://test.xxxxxxxxxxx.com/API_v1/WCFService.svc ' using Ws-metadat
A Exchange or DISCO.
Generating files ...
D:\workspace\WCFService.cs
D:\workspace\output.config
#引用System. ServiceModel.dll Compile WCFService.cs build WCFService.dll
PS d:\workspace> c:\windows\microsoft.net\framework\v3.5\csc.exe/t:library wcfservice.cs/r: ". \ System.ServiceModel.dll "
Microsoft (r) Visual C # 2008 Compiler 3.5.30729.5420 for Microsoft (r). NET Framework version 3.5
Version
Copyright (C) Microsoft Corporation. All rights reserved.
#当前路径保存
PS d:\workspace> $current _directory = get-location
#当前路径显示
PS d:\workspace> $current _directory
Path
----
D:\workspace
#加载System.ServiceModel.dll
PS d:\workspace> [System.reflection.assembly]::loadfrom ("$current _directory\system.servicemodel.dll")
GAC Version Location
--- ------- --------
True v4.0.30319 C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\ ...
#加载WCFService.dll
PS d:\workspace> [System.reflection.assembly]::loadfrom ("$current _directory\WCFService.dll")
GAC Version Location
--- ------- --------
False v2.0.50727 D:\workspace\WCFService.dll
#设置环境的配置config文件
PS D:\workspace > [System.appdomain]::currentdomain.setdata ("App_config_file", "$current _directory\output.config")
PS d:\workspace> if ($psISE-ne $null) {
>> add-type-assemblyname System.Configuration
>> [Configuration.configurationmanager]. GetField ("S_initstate", "NonPublic, Static"). SetValue ($null, 0)
>> [Configuration.configurationmanager]. GetField ("S_configsystem", "NonPublic, Static"). SetValue ($null, $null)
>> ([Configuration.configurationmanager]. Assembly.gettypes () | where {$_. Fullname-eq "System.Configuration.ClientCo
nfigpaths"}) [0]. GetField ("S_current", "NonPublic, Static"). SetValue ($null, $null)
>> }
>>
#连接WCF, get the service object
# (Wcfserviceclient is the class name in WCFService.cs and is a subclass of System.ServiceModel.ClientBase)
# ("Custombinding_iwcfservice" is the value of the Output.config endpoint element name property)
PS d:\workspace> $service = new-object wcfserviceclient("custombinding_iwcfservice")
#设置验证用户名 (available on the server side)
PS d:\workspace> $service. Clientcredentials.username.username= "user001"
#设置 Authentication password (available on the server side)
PS d:\workspace> $service. clientcredentials.username.password= "password001"
#调用服务对象方法 (when the parameter has a complex type, the complex type is defined in WCFService.cs, and the following method is passed in after instantiation )
PS d:\workspace> $service. GetInfo ("001", "1")
※ Reprint Please indicate the source http://i.cnblogs.com/EditPosts.aspx?postid=5316333
Use PowerShell to invoke WCF with Config file config (with username password Authentication)