问题
../Iservice1.cs/
public interface IService1
{
[OperationContract]
Int32 Add(Int32 Num1, Int32 Num2);
}
../Iservice1.svc.cs/
public class Service1 : IService1
{
public Int32 Add(Int32 Num1, Int32 Num2)
{
return Num1 + Num2;
}
}
I created the service. I opened a project in Javada and added the service. But how can I call the service in java "add" method.?
SOLVE:
public class JavaApplication {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
MathService service = new MathService();
IMathService serv;
serv = service.getBasicHttpBindingIMathService();
int x=8, y=2;
int ans;
ans=serv.add(x, y);
System.out.println(ans);
// TODO code application logic here
}
}
回答1:
Take the IntelliJ IDEA as an example.
In Java, there is a webservice
client library. It could help us generate the client java code, then we use the client to call the service.
It prompts us to input the WSDL
page when the project is opened, WCF usually publish the complete WSDL
file by SingleWSDL
instead of WSDL
page. The SingleWSDL
contains all service definitions in one file. Here we input SingleWSDL
URL.
We could also take advantage of Tools Menu to add the Webservice client java code.
The ServiceLocator
class includes our client java code. We can use automatically generated HelloWolrdClient.java
to simulate the invocation, Press Alt+enter
to import the jar package.
At last, please do not forget to modify the service URL, the default is localhost
.
Run the Main method in HelloWorldClient
. Result.
Feel free to let me know if there is anything I can help with.
来源:https://stackoverflow.com/questions/59404484/c-sharp-wcf-service-how-to-consume-call-java-client