下载thrift:http://thrift.apache.org/download
解压打开E:\C#\thrift0.9.3\thrift-0.9.3\lib\csharp\src,vs打开项目,生成thrift.dll
项目引用thrift.dll
根据thrift文件生成代码,可在thrift文件使用namespace指定命名空间。
thrift命令:thrift -gen csharp -out E:\SOA\Thrift E:\SOA\Thrift\sayhello.thrift
thrift文件内容:
service sayHelloService {
string sayHello(1:string name),
}
消费者C#代码:
//建立socket连接的ip
string TSocketIP = "xxx.xxx.xxx.xxx";
//建立socket连接的port
int TSocketPort = xxx;
string ServiceName = "application名/server端service名/version";
string result = string.Empty;
#region 测试
TTransport transport = new TFramedTransport(new TSocket(TSocketIP, TSocketPort, 1000));
TProtocol protocol = new TCompactProtocol(transport);
TMultiplexedProtocol sayHello = new TMultiplexedProtocol(protocol, ServiceName);
sayHelloService.Client client = new sayHelloService.Client(sayHello);
transport.Open();
bool IsOpen = transport.IsOpen;
string name = string.Empty;
name = "毛毛";
result = pushClient.sayHello(name);
#endregion
以下是cmd命令窗的执行(查看thrift相关参数):
Microsoft Windows [版本 10.0.10240]
(c) 2015 Microsoft Corporation. All rights reserved.
C:\Users\Administrator>cd E:\C#\thrift0.9.3
C:\Users\Administrator>E:
E:\C#\thrift0.9.3>thrift -help
Usage: thrift [options] file
Options:
-version Print the compiler version
-o dir Set the output directory for gen-* packages
(default: current directory)
-out dir Set the ouput location for generated files.
(no gen-* folder will be created)
-I dir Add a directory to the list of directories
searched for include directives
-nowarn Suppress all compiler warnings (BAD!)
-strict Strict compiler warnings on
-v[erbose] Verbose mode
-r[ecurse] Also generate included files
-debug Parse debug trace to stdout
--allow-neg-keys Allow negative field keys (Used to preserve protocol
compatibility with older .thrift files)
--allow-64bit-consts Do not print warnings about using 64-bit constants
--gen STR Generate code with a dynamically-registered generator.
STR has the form language[:key1=val1[,key2[,key3=val3]]].
Keys and values are options passed to the generator.
Many options will not require values.
Options related to audit operation
--audit OldFile Old Thrift file to be audited with 'file'
-Iold dir Add a directory to the list of directories
searched for include directives for old thrift file
-Inew dir Add a directory to the list of directories
searched for include directives for new thrift file
Available generators (and options):
as3 (AS3):
bindable: Add [bindable] metadata to all the struct classes.
c_glib (C, using GLib):
cocoa (Cocoa):
log_unexpected: Log every time an unexpected field ID or type is encountered.
validate_required:
Throws exception if any required field is not set.
async_clients: Generate clients which invoke asynchronously via block syntax.
cpp (C++):
cob_style: Generate "Continuation OBject"-style classes.
no_client_completion:
Omit calls to completion__() in CobClient class.
no_default_operators:
Omits generation of default operators ==, != and <
templates: Generate templatized reader/writer methods.
pure_enums: Generate pure enums instead of wrapper classes.
include_prefix: Use full include paths in generated files.
moveable_types: Generate move constructors and assignment operators.
csharp (C#):
async: Adds Async support using Task.Run.
asyncctp: Adds Async CTP support using TaskEx.Run.
wcf: Adds bindings for WCF to generated classes.
serial: Add serialization support to generated classes.
nullable: Use nullable types for properties.
hashcode: Generate a hashcode and equals implementation for classes.
union: Use new union typing, which includes a static read function for union types.
d (D):
delphi (delphi):
ansistr_binary: Use AnsiString for binary datatype (default is TBytes).
register_types: Enable TypeRegistry, allows for creation of struct, union
and container instances by interface or TypeInfo()
constprefix: Name TConstants classes after IDL to reduce ambiguities
events: Enable and use processing events in the generated code.
xmldoc: Enable XMLDoc comments for Help Insight etc.
erl (Erlang):
legacynames: Output files retain naming conventions of Thrift 0.9.1 and earlier.
maps: Generate maps instead of dicts.
otp16: Generate non-namespaced dict and set instead of dict:dict and sets:set.
go (Go):
package_prefix= Package prefix for generated files.
thrift_import= Override thrift package import path (default:git.apache.org/thrift.git/lib/go/thrift)
package= Package name (default: inferred from thrift file name)
ignore_initialisms
Disable automatic spelling correction of initialisms (e.g. "URL")
read_write_private
Make read/write methods private, default is public Read/Write
gv (Graphviz):
exceptions: Whether to draw arrows from functions to exception.
haxe (Haxe):
callbacks Use onError()/onSuccess() callbacks for service methods (like AS3)
rtti Enable @:rtti for generated classes and interfaces
buildmacro=my.macros.Class.method(args)
Add @:build macro calls to generated classes and interfaces
hs (Haskell):
html (HTML):
standalone: Self-contained mode, includes all CSS in the HTML files.
Generates no style.css file, but HTML files will be larger.
noescape: Do not escape html in doc text.
java (Java):
beans: Members will be private, and setter methods will return void.
private-members: Members will be private, but setter methods will return 'this' like usual.
nocamel: Do not use CamelCase field accessors with beans.
fullcamel: Convert underscored_accessor_or_service_names to camelCase.
android: Generated structures are Parcelable.
android_legacy: Do not use java.io.IOException(throwable) (available for Android 2.3 and above).
option_type: Wrap optional fields in an Option type.
java5: Generate Java 1.5 compliant code (includes android_legacy flag).
reuse-objects: Data objects will not be allocated, but existing instances will be used (read and write).
sorted_containers:
Use TreeSet/TreeMap instead of HashSet/HashMap as a implementation of set/map.
generated_annotations=[undated|suppress]:
undated: suppress the date at @Generated annotations
suppress: suppress @Generated annotations entirely
javame (Java ME):
js (Javascript):
jquery: Generate jQuery compatible code.
node: Generate node.js compatible code.
ts: Generate TypeScript definition files.
json (JSON):
merge: Generate output with included files merged
lua (Lua):
ocaml (OCaml):
perl (Perl):
php (PHP):
inlined: Generate PHP inlined files
server: Generate PHP server stubs
oop: Generate PHP with object oriented subclasses
rest: Generate PHP REST processors
nsglobal=NAME: Set global namespace
validate: Generate PHP validator methods
json: Generate JsonSerializable classes (requires PHP >= 5.4)
py (Python):
new_style: Generate new-style classes.
twisted: Generate Twisted-friendly RPC services.
tornado: Generate code for use with Tornado.
utf8strings: Encode/decode strings using utf8 in the generated code.
coding=CODING: Add file encoding declare in generated file.
slots: Generate code using slots for instance members.
dynamic: Generate dynamic code, less code generated but slower.
dynbase=CLS Derive generated classes from class CLS instead of TBase.
dynexc=CLS Derive generated exceptions from CLS instead of TExceptionBase.
dynimport='from foo.bar import CLS'
Add an import line to generated code to find the dynbase class.
rb (Ruby):
rubygems: Add a "require 'rubygems'" line to the top of each generated file.
namespaced: Generate files in idiomatic namespaced directories.
st (Smalltalk):
xsd (XSD):